Take a look at the following loop. This loop calls a function in each iteration and the function does some stuffs. Can you think of any performance improvement idea?
for(var i=0; i<count; ++i)
processElement(elements[i]);
Well, for sufficiently large array, function delegates may result in significant performance improvement to the loop.
var delegate = processElement;
for(var i=0; i<count; ++i)
delegate(elements[i]);
The reason behind performance improvement is, JavaScript interpreter will use the function as local variable and will not lookup in its scope chain for the function body in each iteration.
No comments:
Post a Comment