Skip to content

Instantly share code, notes, and snippets.

@ry
Created March 12, 2012 00:17
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save ry/2018811 to your computer and use it in GitHub Desktop.
Save ry/2018811 to your computer and use it in GitHub Desktop.
a proper fibonacci server in node. it will light up all your cores.
var http = require('http')
var fork = require('child_process').fork;
function fib(n) {
if (n < 2) {
return 1;
} else {
return fib(n - 2) + fib(n - 1);
}
}
if (process.argv[2] == 'fib') {
var r = fib(40);
process.send({ result: r });
process.exit(0);
} else {
var server = http.createServer(function(req, res) {
var child = fork(__filename, [ 'fib' ]);
child.on('message', function(m) {
res.writeHead(200);
res.end(m.result + "\n");
});
});
server.listen(8000);
console.log("server online at http://localhost:8000/")
}
@3rd-Eden
Copy link

OMG and heres a version with just one line of code

var http=require("http"),pool=require("threads_a_gogo").createPool(5).all.eval(fib);function fib(a){return 2>a?1:fib(a-2)+fib(a-1)}var server=http.createServer(function(a,b){pool.any.eval("fib(40)",function(a,c){b.writeHead(200);b.end(c+"\n")})});server.listen(8E3);console.log("server online at http://localhost:8000/");

Point being.. Lines of code are pointless.

@kybernetyk
Copy link

oh god, node fanboys make me wanna puke :[

@hems
Copy link

hems commented Nov 5, 2014

@kybernetyk your profile just made me puke

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