Create a gist now

Instantly share code, notes, and snippets.

@ry /fib.js
Created Mar 12, 2012

What would you like to do?
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/")
}
@DAddYE

This comment has been minimized.

Show comment Hide comment
@DAddYE

DAddYE Mar 12, 2012

Great, but Im still curious to know why people tell you to make it :D

DAddYE commented Mar 12, 2012

Great, but Im still curious to know why people tell you to make it :D

@yesidiaz

This comment has been minimized.

Show comment Hide comment
@yesidiaz

yesidiaz Mar 12, 2012

:) cool

:) cool

@sprjr

This comment has been minimized.

Show comment Hide comment
@sprjr

sprjr Mar 12, 2012

@DAddYE an article awhile back was written showcasing poor node.js performance compared to other languages using fibonacci calculation as the example. It was counter argued that the node.js server was poorly written, thus flawing the results. That would be my guess as to why @ry posted a proper one.

sprjr commented Mar 12, 2012

@DAddYE an article awhile back was written showcasing poor node.js performance compared to other languages using fibonacci calculation as the example. It was counter argued that the node.js server was poorly written, thus flawing the results. That would be my guess as to why @ry posted a proper one.

@phyreman

This comment has been minimized.

Show comment Hide comment
@phyreman

phyreman Mar 12, 2012

Here's a version that actually accounts for the input instead of always looking for "fib". Plus it makes the fib() function smaller: https://gist.github.com/2023979

Here's a version that actually accounts for the input instead of always looking for "fib". Plus it makes the fib() function smaller: https://gist.github.com/2023979

@DAddYE

This comment has been minimized.

Show comment Hide comment
@DAddYE

DAddYE Mar 12, 2012

Thanks @sprjr!

DAddYE commented Mar 12, 2012

Thanks @sprjr!

@tarcieri

This comment has been minimized.

Show comment Hide comment
@tarcieri

tarcieri Mar 12, 2012

Why don't you spin up a fixed-sized pool of long-lived worker processes that matches the number of CPU cores in the system?

By spawning new processes every time, at the very least you're making V8's JIT recompile the fibonacci function for every request, where that wouldn't be an issue with long-lived workers

Why don't you spin up a fixed-sized pool of long-lived worker processes that matches the number of CPU cores in the system?

By spawning new processes every time, at the very least you're making V8's JIT recompile the fibonacci function for every request, where that wouldn't be an issue with long-lived workers

@ry

This comment has been minimized.

Show comment Hide comment
@ry

ry Mar 13, 2012

@tarcieri yeah, that would be better

Owner

ry commented Mar 13, 2012

@tarcieri yeah, that would be better

@teddziuba

This comment has been minimized.

Show comment Hide comment
@teddziuba

teddziuba Mar 13, 2012

@ry that would be Apache.

@ry that would be Apache.

@ry

This comment has been minimized.

Show comment Hide comment
@ry

ry Mar 13, 2012

in two dozen lines of javascript...

Owner

ry commented Mar 13, 2012

in two dozen lines of javascript...

@xk

This comment has been minimized.

Show comment Hide comment
@xk

xk Mar 13, 2012

@ry : the threads_a_gogo version: https://gist.github.com/2031338 is only nineteen lines :

var http = require('http')
var pool = require('threads_a_gogo').createPool(5).all.eval(fib);

function fib (n) {
  if (n < 2) {
    return 1;
  } else {
    return fib(n - 2) + fib(n - 1);
  }
}

var server = http.createServer(function(req, res) {
  pool.any.eval('fib(40)', function (err, data) {
    res.writeHead(200);
    res.end(data + "\n");
  });
});
server.listen(8000);
console.log("server online at http://localhost:8000/")

xk commented Mar 13, 2012

@ry : the threads_a_gogo version: https://gist.github.com/2031338 is only nineteen lines :

var http = require('http')
var pool = require('threads_a_gogo').createPool(5).all.eval(fib);

function fib (n) {
  if (n < 2) {
    return 1;
  } else {
    return fib(n - 2) + fib(n - 1);
  }
}

var server = http.createServer(function(req, res) {
  pool.any.eval('fib(40)', function (err, data) {
    res.writeHead(200);
    res.end(data + "\n");
  });
});
server.listen(8000);
console.log("server online at http://localhost:8000/")
@xk

This comment has been minimized.

Show comment Hide comment
@xk

xk Mar 13, 2012

@ry : And the threads_a_gogo version is almost twice as fast

$ time curl localhost:8000 //the threads_a_gogo version
165580141

real    0m2.550s
user    0m0.004s
sys 0m0.005s

$ time curl localhost:8000 // @ryah version
165580141

real    0m4.723s
user    0m0.004s
sys 0m0.004s

xk commented Mar 13, 2012

@ry : And the threads_a_gogo version is almost twice as fast

$ time curl localhost:8000 //the threads_a_gogo version
165580141

real    0m2.550s
user    0m0.004s
sys 0m0.005s

$ time curl localhost:8000 // @ryah version
165580141

real    0m4.723s
user    0m0.004s
sys 0m0.004s
@xk

This comment has been minimized.

Show comment Hide comment
@xk

xk Mar 14, 2012

And uses less than 1/4th the memory:

ab -c 10 -n 10 http://127.0.0.1:8000 ->

@ry : 11 node processes, 22 threads
11.0+10.9+11.0+11.1+11.1+11.1+11.0+11.0+11.1+10.8+10.2= 120.3 MB
26.3 seconds
0.38 requests/s

threads_a_gogo: 1 node process, 12 threads
29.5 MB
13.2s
0.75 requests/s

xk commented Mar 14, 2012

And uses less than 1/4th the memory:

ab -c 10 -n 10 http://127.0.0.1:8000 ->

@ry : 11 node processes, 22 threads
11.0+10.9+11.0+11.1+11.1+11.1+11.0+11.0+11.1+10.8+10.2= 120.3 MB
26.3 seconds
0.38 requests/s

threads_a_gogo: 1 node process, 12 threads
29.5 MB
13.2s
0.75 requests/s

@teddziuba

This comment has been minimized.

Show comment Hide comment
@teddziuba

teddziuba Mar 14, 2012

@phyreman

This comment has been minimized.

Show comment Hide comment
@phyreman

phyreman Mar 15, 2012

@ry @xk

And here's a version using threads_a_gogo that uses 14 lines and accepts input: https://gist.github.com/2023979

Pass input via curl localhost:8000/# (i.e. curl localhost:8000/42)

@ry @xk

And here's a version using threads_a_gogo that uses 14 lines and accepts input: https://gist.github.com/2023979

Pass input via curl localhost:8000/# (i.e. curl localhost:8000/42)

@3rd-Eden

This comment has been minimized.

Show comment Hide comment
@3rd-Eden

3rd-Eden Mar 23, 2012

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.

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

This comment has been minimized.

Show comment Hide comment
@kybernetyk

kybernetyk Mar 24, 2012

oh god, node fanboys make me wanna puke :[

oh god, node fanboys make me wanna puke :[

@hems

This comment has been minimized.

Show comment Hide comment
@hems

hems Nov 5, 2014

@kybernetyk your profile just made me puke

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