Created
December 21, 2011 13:12
-
-
Save jsmarkus/1505996 to your computer and use it in GitHub Desktop.
Javascript function-in-function performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = function (v) { | |
function b (v) { | |
return 42; | |
} | |
return b(v); | |
} | |
var start = (new Date).getTime(); | |
for (var i=0; i < 1e6; i++) { | |
a(i); | |
} | |
var t = (new Date).getTime() - start; | |
console.log(t); //122 ms - slow, because each time a() is called - b() seems to be redefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function b (v) { | |
return 42; | |
} | |
var a = function (v) { | |
return b(v); | |
} | |
//---------------------------------------------- | |
var start = (new Date).getTime(); | |
for (var i=0; i < 1e6; i++) { | |
a(i); | |
} | |
var t = (new Date).getTime() - start; | |
console.log(t); //17 ms - fast - because b() is defined only once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = function (v) { | |
var namespace = { | |
b : function (v) { | |
return 42; | |
} | |
} | |
return namespace.b(v); | |
} | |
//---------------------------------------------- | |
var start = (new Date).getTime(); | |
for (var i=0; i < 1e6; i++) { | |
a(i); | |
} | |
var t = (new Date).getTime() - start; | |
console.log(t); //55 ms - the hell why is it faster than case1.js??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this gist I tried to compare the performance of private functions in JS. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment