Skip to content

Instantly share code, notes, and snippets.

@jsmarkus
Created December 21, 2011 13:12
Show Gist options
  • Save jsmarkus/1505996 to your computer and use it in GitHub Desktop.
Save jsmarkus/1505996 to your computer and use it in GitHub Desktop.
Javascript function-in-function performance
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
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
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???
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