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
A comparison of compact memoization routines providing the bare minimum functionality necessary for caching.
<script>
function fib(x) {
if (x < 2) return 1;
else return fib(x - 1) + fib(x - 2);
}
// compact memoizer
function memoize1(param) {
this.memoize = this.memoize || {};
return (param in this.memoize) ? this.memoize[param] : this.memoize[param] = fib(param);
}
// using an external cache
var memCache = {};
function memoize2(param, cache) {
cache = cache || {};
return (param in cache) ? cache[param] : cache[param] = fib(param);
}
// stoyan's memoizer
function memoize3(param) {
if (!memoize3.cache) {
memoize3.cache = {};
}
if (!memoize3.cache[param]) {
var result = fib(param); //custom function
memoize3.cache[param] = result;
}
return memoize3.cache[param];
}
function mem (param) {
this.cache = this.cache || {}
return this.cache[param] || (this.cache[param] = fib(param))
}
function memOne (param) {
return (this.cache = this.cache || {}) &&
this.cache[param] || (this.cache[param] = fib(param))
}
var m1,m2,m3,m4,m5 = 0;
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Compact memoize.js |
| ready |
Stoyan's version |
| ready |
With external cache |
| ready |
small mem |
| ready |
oneline mem |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.