Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Memoize limit #281

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 47 additions & 5 deletions packages/function-memoize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,40 @@ module.exports = memoize;
sum(10, 20) -- Cache hit!
*/

function memoize(callback, resolver) {
function Order() {
this.keys = [];
this.cache = {};
}

Order.prototype = {
'has': function(key) {
return this.cache.hasOwnProperty(key);
},

'get': function(key) {
return this.cache[key];
},

'set': function(key, value) {
if (!this.has(key)) {
this.keys.push(key);
}

this.cache[key] = value;
},

'delete': function(key) {
var index = this.keys.indexOf(key);
this.keys.splice(index, 1);
delete this.cache[key];
},

'keys': function() {
return this.keys;
},
};

function memoize(callback, resolver, options) {
if (typeof callback !== 'function') {
throw new Error('`callback` should be a function');
}
Expand All @@ -32,17 +65,26 @@ function memoize(callback, resolver) {
throw new Error('`resolver` should be a function');
}

var cache = {};
if (options !== undefined && typeof options !== 'object') {
throw new Error('`options` should be an object');
}

var cache = 'Map' in (typeof window === 'undefined' ? global : window) ? new Map() : new Order();

var memoized = function() {
var args = Array.prototype.slice.call(arguments); // to simplify JSON.stringify
var key = resolver ? resolver.apply(this, args) : JSON.stringify(args);

if (!(key in cache)) {
cache[key] = callback.apply(this, args);
if (!cache.has(key)) {
cache.set(key, callback.apply(this, args));
}

if (typeof options.max === 'number' && cache.size > options.max) {
var firstKey = cache.keys()[0];
cache.delete(firstKey);
}

return cache[key];
return cache.get(key);
};

memoized.cache = cache;
Expand Down