Skip to content

Instantly share code, notes, and snippets.

@usagi
Created April 1, 2012 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/2274376 to your computer and use it in GitHub Desktop.
Save usagi/2274376 to your computer and use it in GitHub Desktop.
// ©2012 Usagi Ito / Wonder Rabbit Project, License is MIT/X11
// Usagi Ito <usagi@WonderRabbitProject.net>
// this source delivered from:
// https://gist.github.com/2274188
var main = function(){
const N = 16 * 1024;
console.log( 'N = ' + N );
var a =
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
var benchmark = function(f, a){
var start = new Date();
f();
var end = new Date();
console.log(
( end.getTime() - start.getTime() ) +
' [ms] ' + 'cost of ' + a
);
};
var proc = function(){
var x = a;
};
benchmark(
function(){
var n = N;
while(--n)
proc();
},
N + '-th proc with while loop'
);
benchmark(
function(){
var f = function(n){
proc();
if (--n)
arguments.callee(n);
};
f(N);
},
N + '-th proc with higher-order function(argument.callee)'
);
}
main();
@usagi
Copy link
Author

usagi commented Apr 1, 2012

LH-MAIN /tmp% node benchmark_loop.js
N = 16384
0 [ms] cost of 16384-th proc with while loop
64 [ms] cost of 16384-th proc with higher-order function(argument.callee)

LH-MAIN /tmp% node --version
v0.6.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment