最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it best to declare temporary variable inside or outside of a loop? - Stack Overflow

programmeradmin1浏览0评论

What's the best practice, performance-wise, about temporary variables in loops? Is it better to do something like :

some_array.forEach(function(item) {
  var temp_obj = {};
  // do some operations with temp_obj
});

OR

var temp_obj;
json.forEach(function(item) {
  temp_obj = {};
  // do some operations with temp_obj
});

I always assumed there was no difference in performance, but every time I use it I'm not sure I'm following the good practices...

What's the best practice, performance-wise, about temporary variables in loops? Is it better to do something like :

some_array.forEach(function(item) {
  var temp_obj = {};
  // do some operations with temp_obj
});

OR

var temp_obj;
json.forEach(function(item) {
  temp_obj = {};
  // do some operations with temp_obj
});

I always assumed there was no difference in performance, but every time I use it I'm not sure I'm following the good practices...

Share Improve this question asked Nov 14, 2013 at 19:07 Samuel BolducSamuel Bolduc 19.2k8 gold badges36 silver badges58 bronze badges 2
  • 2 @user2356705 that question represents a very different situation. – Pointy Commented Nov 14, 2013 at 19:12
  • 1 IMO, keep the varibles in the nearest scope as possible, so in this case, inside the callback. – Blue Skies Commented Nov 14, 2013 at 19:17
Add a ment  | 

4 Answers 4

Reset to default 2

The first two ments are very wrong (at time of writing, some have since been deleted). Since forEach takes a function as an argument, a closure is formed and variable hoisting doesn't apply.

To answer your question: depends on the context. You shouldn't use global variables, but if your code from the second example is wrapped inside some other function, it's preferable, since the variable is only allocated once (incidentally, number 2 illustrates the concept of closures well, if wrapped inside a parent function).

Depends of what kind of performance we are talking about. In any case I doubt any of those will show a noticeable difference. Also keep in mind that the engine might apply optimizations and the generated might actually differ.


some_array.forEach(function(item) {
  var temp_obj = {};
  // do some operations with temp_obj
});

In this case the variable temp_obj will be faster to look up because it is defined in local scope. This is probably preferable.


var temp_obj;
json.forEach(function(item) {
  temp_obj = {};
  // do some operations with temp_obj
});

Creation time of the function might be less because a local variable doesn't have to be created.


And of course there is also a behavioral difference between local variables and free variables. Free variables will be "shared" between each function call.

There are no good practices it all depends on the scope you are trying to achieve.

Inside the loop means your var will not be accessible outside the loop. There are also a few other things to get in relation with closure. one resource for instance

This is actually interesting question, mainly because of the way that javascript handles scoping. There are a couple of ments that mention that this question has been answered previously, but it doesn't appear to me that it has. Note that there is a big difference between this:

for(var i = 0; i < someArray.length; i++){}

and this:

someArray.forEach(function(item){});

because of the fact that the callback in the second one provides scope. If you are using a for loop, it literally doesn't matter if you declare the variable inside or outside of the function because the variable will be hoisted up out of the loop anyway.

In the case that you are specifically asking about, whether or not to declare the variable inside or outside of the function matters because the function provides scope, and the variable will not be hoisted. If you care about performance, you should declare it outside of the callback, since the variable will only be created once. If you do not need to use the variable outside of the callback, however, I would actually remend defining the variable within the callback. In my opinion, it makes the code easier to read and unless your temp object is very memory expensive, it won't effect performance in any noticeable way.

发布评论

评论列表(0)

  1. 暂无评论