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.
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];
}
//latest gratest sept 9th 2014 memoize.js
var memoize4 = function(func) {
var stringifyJson = JSON.stringify,
cache = {};
var cachedfun = function() {
var hash = stringifyJson(arguments);
return (hash in cache) ? cache[hash] : cache[hash] = func.apply(this, arguments);
};
cachedfun.__cache = (function() {
cache.remove || (cache.remove = function() {
var hash = stringifyJson(arguments);
return (delete cache[hash]);
});
return cache;
}).call(this);
return cachedfun;
};
var m1, m2, m3, m4 = 0;
Ready to run.
Test | Ops/sec | |
---|---|---|
Compact memoize.js |
| ready |
Stoyan's version |
| ready |
With external cache |
| ready |
memoize.js |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.