Skip to content

Instantly share code, notes, and snippets.

@19h
Created April 10, 2013 14:37
Show Gist options
  • Select an option

  • Save 19h/5355190 to your computer and use it in GitHub Desktop.

Select an option

Save 19h/5355190 to your computer and use it in GitHub Desktop.
Ultra High-Speed Fibonacci Generator. This is faster then the addition-increment approach in C or ASM.
fib = function (c) {
var b, a = [0, 1];
for (b = 0; b < c; ++b) a.push(a[0] + a[1]), a.shift();
return a[0]
};
@19h
Copy link
Author

19h commented Apr 10, 2013

var a = {
        a: {
                "0": 0,
                1: 1
        },
        b: function (b) {
                if (void 0 !== a.a[b]) return a.a[b];
                a.a[b] = a.b(b - 1) + a.b(b - 2);
                return a.a[b]
        }
};

//a.b([int])

@19h
Copy link
Author

19h commented Apr 10, 2013

An last but not least:

v = function (d) {
        var a = 0,
                b = 1,
                c = 0,
                e = [0, 1],
                f = 1;
        do e.push(a = c + b), c = b, b = a; while (++f < d);
        return a
};

@19h
Copy link
Author

19h commented Jun 27, 2013

Benchmark:

Approach B: [ 0, 22516 Nanoseconds ] 1000th Fibonacci
Approach C: [ 0, 62743 Nanoseconds ] 1000th Fibonacci
Approach A: [ 0, 159227 Nanoseconds ] 1000th Fibonacci

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