Skip to content

Instantly share code, notes, and snippets.

@xk
Created April 11, 2012 21:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xk/2362548 to your computer and use it in GitHub Desktop.
Save xk/2362548 to your computer and use it in GitHub Desktop.
When threads_a_gogo beats node 2 to 1
/*
$ node bench.js
Threads_a_gogo JS thread -> 4624 (ms) 298607040
Node's main JS thread -> 8471 (ms) 298607040
Ratio: 1.83 times faster
*/
function fib (n) {
return (n < 2) ? 1 : fib(n-2)+ fib(n-1);
}
function loop (n) {
var r= 0;
while (n--) r+= fib(35);
return r;
}
function cb (err, data) {
var save_t= t= Date.now()- t;
console.log("Threads_a_gogo JS thread -> ", t, '(ms) ', data);
thread.destroy();
t= Date.now();
data= loop(20);
t= Date.now()- t;
console.log("Node's main JS thread -> ", t, '(ms) ', data);
console.log('Ratio: '+ (t/save_t).toFixed(2)+ ' times faster');
}
var thread= require('threads_a_gogo').create().eval(fib).eval(loop);
var t= Date.now();
thread.eval('loop(20)', cb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment