Skip to content

Instantly share code, notes, and snippets.

@zakki
Created November 9, 2015 05:00
Show Gist options
  • Save zakki/a5a93ceec008446032eb to your computer and use it in GitHub Desktop.
Save zakki/a5a93ceec008446032eb to your computer and use it in GitHub Desktop.
fib
$ time node fib.asm.js
102334155
node fib.asm.js 0.00s user 0.01s system 0% cpu 1.723 total
$ time node fib.js
102334155
node fib.js 0.00s user 0.01s system 0% cpu 2.025 total
$ cat fib.asm.js
function Module() {
'use asm';
function fib(n) {
n = n | 0;
var a = 0, b = 0, r = 0;
if ((n | 0) <= 1) {
return n | 0;
}
a = n - 1 | 0;
b = n - 2 | 0;
r = (fib(a) | 0) + (fib(b) | 0) | 0;
return r | 0;
}
function main() {
return fib(40) | 0;
}
return {
fib: fib,
main: main
};
}
var module = Module();
console.log(module.main());
$ cat fib.js
function fib(n){
if(n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
console.log(fib(40));
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment