wayofthefuture
wayofthefuture

Reputation: 9395

Effect of function location on overall performance

I have a rather large javascript application and am trying to optimize performance. If I have a loop that will execute a small function thousands of times, does putting the small function far away, code-wise, from the calling function have any performance implications? Thank You.

Upvotes: 3

Views: 86

Answers (1)

Talha Akbar
Talha Akbar

Reputation: 10030

There is no difference declaring the function in loop or calling it after each iteration. I have been taught that each function and variable according to its size creates its space in RAM at specified location. Javascript knows where that function or variable is located in memory because we assign it a name like foo.

for(var i = 0; i < 1000; i++) foo(i);
... Your 300 lines
function foo(i) {
   document.body.innerHTML += i+"<br />";
}

or

for(var i = 0; i < 1000; i++) {
   document.body.innerHTML += i+"<br />";
}

You can use the way you like. The functions when declared have fixed position in memory thus can be called from anywhere. You can also call it from Europe if it is located there.

Upvotes: 7

Related Questions