between saving a key name like this
for(var key in this.data)
{
var key_name = key;
for(key in this.data[key_name].content){
alert(this.data[key_name].content[key].score);
}
}
or making checkpoint objects for every parent node
for(var key in this.data)
{
var obj = this.data[key];
for(key in obj.content){
var inner_obj = obj.content;
alert(inner_obj[key].score);
}
}
which one has better performance? any other suggestion?
between saving a key name like this
for(var key in this.data)
{
var key_name = key;
for(key in this.data[key_name].content){
alert(this.data[key_name].content[key].score);
}
}
or making checkpoint objects for every parent node
for(var key in this.data)
{
var obj = this.data[key];
for(key in obj.content){
var inner_obj = obj.content;
alert(inner_obj[key].score);
}
}
which one has better performance? any other suggestion?
Share Improve this question edited Mar 24, 2012 at 18:41 tkone 22.8k5 gold badges57 silver badges78 bronze badges asked Mar 24, 2012 at 16:33 lngslngs 6981 gold badge8 silver badges17 bronze badges 8- 6 Why don't you try this yourself? – Tamara Wijsman Commented Mar 24, 2012 at 16:34
- 7 jsperf. – j08691 Commented Mar 24, 2012 at 16:36
- 6 Hey, guys, this is stackoverflow, not only a forum. It is intented to be a knowledge resource. So if you don't know, just ignore the question. That's my point of view. +1 – yunzen Commented Mar 24, 2012 at 16:38
- 1 @yunzen with questions like this, there is no "know." There is only "measure." – Matt Ball Commented Mar 24, 2012 at 16:43
- 4 The first rule of thumb is that if you really need to know the answer to a performance question, you have to benchmark it yourself to measure. As others have suggested, jsperf is a very useful tool for this type of thing. The second rule of thumb is that if you are going to be dereferencing a property more than once in a loop, then it's probably wise to store the value in a local variable and use the local variable. Local variables are the fastest type of variable access. – jfriend00 Commented Mar 24, 2012 at 16:46
4 Answers
Reset to default 5Only one way to know for sure: measure it.
http://jsperf./so-question-9853395
(click through for up-to-date results)
You should use a bination of the two:
for(var key in this.data)
{
var inner_obj = this.data[key].content;
for(ikey in inner_obj){
alert(inner_obj[ikey].score);
}
}
This would be the fastest way to do it pared to your two proposed solutions.
Please note that I renamed key
to ikey
inside the inner loop to avoid confusion.
After verification, my solution is indeed the fastest:
(source: minus.)
Theoretically at least, the fastest would be to get the object reference outside the inner loop. In practice the difference might not be that big, if the browser is already doing this internally. Also, the performance might differ between browsers.
for(var key in this.data) {
var obj = this.data[key].content;
for(var key2 in obj){
alert(obj[key2].score);
}
}
Note that you should use separate variables for the loops, otherwise it will be hard to follow which values is in the variable at which point when you put some actual code in the loops.
Edit:
When I measure the performance in IE and Firefox, I find that this method is slightly faster than the methods proposed in the question. However, the difference is so small that you should not be concerned with it. Either method works just fine, and any performance concerns would be what you actually do with the values inside the inner loop.
From an code interpreter's point of view, the first one should be faster. In the second example every loop you have to access the this.data
array.
In modern browsers you might have a good Just In Time piler. If this is true, this JIT could possibly 'translate' your code in the exact same opcode.
So you have to test in different browsers by executing your code several times (I mean several thousand times) and measure the execution time.
If you don't have a JIT piler in your browser folloding would be even faster.
var data = this.data
for(var keyA in data)
{
var content = data[keyA].content;
for(keyB in content){
alert(content[keyB].score);
}
}