Does use of anonymous functions affect performance? Does use of anonymous functions affect performance? javascript javascript

Does use of anonymous functions affect performance?


The performance problem here is the cost of creating a new function object at each iteration of the loop and not the fact that you use an anonymous function:

for (var i = 0; i < 1000; ++i) {        myObjects[i].onMyEvent = function() {        // do something        };}

You are creating a thousand distinct function objects even though they have the same body of code and no binding to the lexical scope (closure). The following seems faster, on the other hand, because it simply assigns the same function reference to the array elements throughout the loop:

function myEventHandler() {    // do something}for (var i = 0; i < 1000; ++i) {    myObjects[i].onMyEvent = myEventHandler;}

If you were to create the anonymous function before entering the loop, then only assign references to it to the array elements while inside the loop, you will find that there is no performance or semantic difference whatsoever when compared to the named function version:

var handler = function() {    // do something    };for (var i = 0; i < 1000; ++i) {        myObjects[i].onMyEvent = handler;}

In short, there is no observable performance cost to using anonymous over named functions.

As an aside, it may appear from above that there is no difference between:

function myEventHandler() { /* ... */ }

and:

var myEventHandler = function() { /* ... */ }

The former is a function declaration whereas the latter is a variable assignment to an anonymous function. Although they may appear to have the same effect, JavaScript does treat them slightly differently. To understand the difference, I recommend reading, “JavaScript function declaration ambiguity”.

The actual execution time for any approach is largely going to be dictated by the browser's implementation of the compiler and runtime. For a complete comparison of modern browser performance, visit the JS Perf site


Here's my test code:

var dummyVar;function test1() {    for (var i = 0; i < 1000000; ++i) {        dummyVar = myFunc;    }}function test2() {    for (var i = 0; i < 1000000; ++i) {        dummyVar = function() {            var x = 0;            x++;        };    }}function myFunc() {    var x = 0;    x++;}document.onclick = function() {    var start = new Date();    test1();    var mid = new Date();    test2();    var end = new Date();    alert ("Test 1: " + (mid - start) + "\n Test 2: " + (end - mid));}

The results:
Test 1: 142msTest 2: 1983ms

It appears that the JS engine doesn't recognise that it's the same function in Test2 and compiles it each time.


As a general design principle, you should avoid implimenting the same code multiple times. Instead you should lift common code out into a function and execute that (general, well tested, easy to modify) function from multiple places.

If (unlike what you infer from your question) you are declaring the internal function once and using that code once (and have nothing else identical in your program) then an anonomous function probably (thats a guess folks) gets treated the same way by the compiler as a normal named function.

Its a very useful feature in specific instances, but shouldn't be used in many situations.