var memoize = function(fn, context) {
var cache = {};
var pContext = (typeof context !== "undefined") ? context : null;
return function() {
var key = Array.prototype.join.call(arguments, "__");
(typeof cache[key] === "undefined" ) && (cache[key] = fn.apply(pContext, arguments));
return cache[key];
}
};
var dirtyFunction = function() {
// just a little something that will take a long time (change loop var to test)
let x = 0;
for(var i = 0; i < 100; i++) x++;
return x;
};
var pow = memoize(dirtyFunction);
// using raw function
var start = new Date(), end, x, max = 10000000;
for (var i = 0; i < max; i++) {
x = dirtyFunction();
}
end = new Date();
console.log('raw runtime', end - start, x);
// using memoized version (w/ caching), literally 1000x slower
start = new Date();
for (i = 0; i < max; i++) {
x = pow();
}
end = new Date();
console.log('memoized runtime', end - start, x);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.