jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
This test demonstrates the expressive power of closures. It is not written for pure performance comparison! In fact, caching the results when computing even larger fibonacci numbers, will be zillion times faster.
Caching of intermediate results could be achieved through other means, e.g. polluting the global namespace with an array, that your function uses for caching. But closures are an elegant way to tie some internal state to a function, that resides over all invocations.
<script>
var fib_plain = function (n) {
return (n <= 1) ? n :
fib_plain(n - 2) + fib_plain(n - 1);
},
fib_closure = function () {
var cache = [];
return function (n) {
return (cache[n]) ? cache[n] :
(n <= 1) ? n :
(cache[n] = arguments.callee(n - 2) + arguments.callee(n - 1))
};
};
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Plain Recursion |
| ready |
Closure Caching |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.