Last active
December 31, 2015 11:19
-
-
Save jcmoore/7978608 to your computer and use it in GitHub Desktop.
https://github.com/joyent/node/issues/6715 (see http://neversaw.us/2013/09/04/on-the-performance-of-closures-in-v8/)
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 z = 1; | |
var add = (function add__ ($$) { | |
return function add (x, y) { | |
var __ = $$; | |
//__.z = -__.z; | |
return x + y + __.z; | |
}; | |
}) ({ | |
get z () { return z; }, | |
set z (__) { z = __; }, | |
}); | |
var f = (function f__ ($$) { | |
return function f () { | |
var __ = $$; | |
var add = __.add; | |
var sum = 0; | |
for (var i = 0; i < 10000; i++) { | |
sum = add(sum, i); | |
//sum = __.add(sum, i); // takes 100x as long !!!!! | |
} | |
//if (sum != 49995000) throw "argh"; | |
}; | |
}) ({ | |
get add () { return add; }, | |
}); | |
function measure(f) { | |
var start = new Date().valueOf(); | |
for (var i = 0; i < 100000; i++) { | |
// invoke f() with f_this as "this" -- thus exposing the add() method | |
f(); | |
} | |
var end = new Date().valueOf(); | |
if (typeof console !== "undefined") { | |
console.log(end - start); | |
} else { | |
print(end - start); | |
} | |
} | |
measure(f); // (10s on node, 2.6s on node-webkit, 1.6s on Chrome, 1.7s on d8) | |
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 z = 1; | |
var add = (function add__ ($$) { | |
var a = function add (x, y) { | |
var __ = this; | |
//__.z = -__.z; | |
return x + y + __.z; | |
}; | |
a.__ = $$; | |
$$.__ = a; | |
return a; | |
}) ({ | |
get z () { return z; }, | |
set z (__) { z = __; }, | |
}); | |
var f = (function f__ ($$) { | |
var b = function f () { | |
var __ = this; | |
var lax; | |
var add = __.add; | |
var sum = 0; | |
for (var i = 0; i < 10000; i++) { | |
sum = add.__.__(sum, i); | |
//sum = (lax = __.add)(sum, i); // takes 100x as long !!!!! | |
} | |
//if (sum != 49995000) throw "argh"; | |
}; | |
b.__ = $$; | |
$$.__ = b; | |
return b; | |
}) ({ | |
get add () { return add; }, | |
}); | |
function measure(f) { | |
var start = new Date().valueOf(); | |
for (var i = 0; i < 100000; i++) { | |
// invoke f() with f_this as "this" -- thus exposing the add() method | |
f.__.__(); | |
} | |
var end = new Date().valueOf(); | |
if (typeof console !== "undefined") { | |
console.log(end - start); | |
} else { | |
print(end - start); | |
} | |
} | |
measure(f); // (1.6s on node, 1.6s on node-webkit, 2.4s on Chrome, 1.7s on d8) | |
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 z = 1; | |
var add = (function add__ ($$) { | |
return function add (x, y) { | |
var __ = $$; | |
//__.z = -__.z; | |
return x + y + __.z; | |
}; | |
}) ({ | |
z: z, | |
}); | |
var f = (function f__ ($$) { | |
return function f () { | |
var __ = $$; | |
var add = __.add; | |
var sum = 0; | |
for (var i = 0; i < 10000; i++) { | |
sum = add(sum, i); | |
//sum = __.add(sum, i); // takes 100x as long !!!!! | |
} | |
//if (sum != 49995000) throw "argh"; | |
}; | |
}) ({ | |
add: add, | |
}); | |
function measure(f) { | |
var start = new Date().valueOf(); | |
for (var i = 0; i < 100000; i++) { | |
// invoke f() with f_this as "this" -- thus exposing the add() method | |
f(); | |
} | |
var end = new Date().valueOf(); | |
if (typeof console !== "undefined") { | |
console.log(end - start); | |
} else { | |
print(end - start); | |
} | |
} | |
measure(f); // (6.9s on node, 1.6s on node-webkit, 1.6s on Chrome, 1.7s on d8) | |
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 z = 1; | |
function add(x, y) { | |
return x + y + z; | |
} | |
function f() { | |
var sum = 0; | |
for (var i = 0; i < 10000; i++) { | |
sum = add(sum, i); | |
} | |
//if (sum != 49995000) throw "argh"; | |
} | |
function measure(f) { | |
var start = new Date().valueOf(); | |
for (var i = 0; i < 100000; i++) { | |
f(); | |
} | |
var end = new Date().valueOf(); | |
if (typeof console !== "undefined") { | |
console.log(end - start); | |
} else { | |
print(end - start); | |
} | |
} | |
measure(f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment