style: Prettify

This commit is contained in:
Mariusz Nowak
2024-05-23 21:55:17 +02:00
parent 924f92bad2
commit fe3e4922a7
41 changed files with 2548 additions and 3000 deletions

211
README.md
View File

@@ -13,19 +13,19 @@ Memoization is best technique to save on memory or CPU cycles when we deal with
### Features
* Works with any type of function arguments **no serialization is needed**
* Works with [**any length of function arguments**](#arguments-length). Length can be set as fixed or dynamic.
* One of the [**fastest**](#benchmarks) available solutions.
* Support for [**promises**](#promise-returning-functions) and [**asynchronous functions**](#nodejs-callback-style-functions)
* [**Primitive mode**](#primitive-mode) which assures fast performance when arguments are convertible to strings.
* [**WeakMap based mode**](#weakmap-based-configurations) for garbage collection friendly configuration
* Can be configured [**for methods**](#memoizing-methods) (when `this` counts in)
* Cache [**can be cleared manually**](#manual-clean-up) or [**after specified timeout**](#expire-cache-after-given-period-of-time)
* Cache size can be **[limited on LRU basis](#limiting-cache-size)**
* Optionally [**accepts resolvers**](#argument-resolvers) that normalize function arguments before passing them to underlying function.
* Optional [**reference counter mode**](#reference-counter), that allows more sophisticated cache management
* [**Profile tool**](#profiling--statistics) that provides valuable usage statistics
* Covered by [**over 500 unit tests**](#tests)
- Works with any type of function arguments **no serialization is needed**
- Works with [**any length of function arguments**](#arguments-length). Length can be set as fixed or dynamic.
- One of the [**fastest**](#benchmarks) available solutions.
- Support for [**promises**](#promise-returning-functions) and [**asynchronous functions**](#nodejs-callback-style-functions)
- [**Primitive mode**](#primitive-mode) which assures fast performance when arguments are convertible to strings.
- [**WeakMap based mode**](#weakmap-based-configurations) for garbage collection friendly configuration
- Can be configured [**for methods**](#memoizing-methods) (when `this` counts in)
- Cache [**can be cleared manually**](#manual-clean-up) or [**after specified timeout**](#expire-cache-after-given-period-of-time)
- Cache size can be **[limited on LRU basis](#limiting-cache-size)**
- Optionally [**accepts resolvers**](#argument-resolvers) that normalize function arguments before passing them to underlying function.
- Optional [**reference counter mode**](#reference-counter), that allows more sophisticated cache management
- [**Profile tool**](#profiling--statistics) that provides valuable usage statistics
- Covered by [**over 500 unit tests**](#tests)
### Installation
@@ -46,9 +46,7 @@ To port it to Browser or any other (non CJS) environment, use your favorite CJS
```javascript
var memoize = require("memoizee");
var fn = function(one, two, three) {
/* ... */
};
var fn = function (one, two, three) { /* ... */ };
memoized = memoize(fn);
@@ -56,7 +54,7 @@ memoized("foo", 3, "bar");
memoized("foo", 3, "bar"); // Cache hit
```
__Note__: Invocations that throw exceptions are not cached.
**Note**: Invocations that throw exceptions are not cached.
### Configuration
@@ -76,7 +74,7 @@ memoized("foo", 3, {}); // Third argument is ignored (but passed to underlying f
memoized("foo", 3, 13); // Cache hit
```
__Note:__ [Parameters predefined with default values (ES2015+ feature)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) are not reflected in function's `length`, therefore if you want to memoize them as well, you need to tweak `length` setting accordingly
**Note:** [Parameters predefined with default values (ES2015+ feature)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) are not reflected in function's `length`, therefore if you want to memoize them as well, you need to tweak `length` setting accordingly
Dynamic _length_ behavior can be forced by setting _length_ to `false`, that means memoize will work with any number of arguments.
@@ -108,8 +106,8 @@ memoized("/path/one"); // Cache hit
By default cache id for given call is resolved either by:
* Direct Comparison of values passed in arguments as they are. In such case two different objects, even if their characteristics is exactly same (e.g. `var a = { foo: 'bar' }, b = { foo: 'bar' }`) will be treated as two different values.
* Comparison of stringified values of given arguments (`primitive` mode), which serves well, when arguments are expected to be primitive values, or objects that stringify naturally do unique values (e.g. arrays)
- Direct Comparison of values passed in arguments as they are. In such case two different objects, even if their characteristics is exactly same (e.g. `var a = { foo: 'bar' }, b = { foo: 'bar' }`) will be treated as two different values.
- Comparison of stringified values of given arguments (`primitive` mode), which serves well, when arguments are expected to be primitive values, or objects that stringify naturally do unique values (e.g. arrays)
Still above two methods do not serve all cases, e.g. if we want to memoize function where arguments are hash objects which we do not want to compare by instance but by its content.
@@ -117,21 +115,21 @@ Still above two methods do not serve all cases, e.g. if we want to memoize funct
There's a `normalizer` option through which we can pass custom cache id normalization function.
###### Memoizing on dictionary (object hash) arguments)
###### Memoizing on dictionary (object hash) arguments)
if we want to memoize a function where argument is a hash object which we do not want to compare by instance but by its content, then we can achieve it as following:
```javascript
var mfn = memoize(
function(hash) {
// body of memoized function
},
{
normalizer: function(args) {
// args is arguments object as accessible in memoized function
return JSON.stringify(args[0]);
}
}
function (hash) {
// body of memoized function
},
{
normalizer: function (args) {
// args is arguments object as accessible in memoized function
return JSON.stringify(args[0]);
},
}
);
mfn({ foo: "bar" });
@@ -142,23 +140,23 @@ If additionally we want to ensure that our logic works well with different order
```javascript
const deepSortedEntries = object =>
Object.entries(object)
.map(([key, value]) => {
if (value && typeof value === "object") return [key, deepSortedEntries(value)];
return [key, value];
})
.sort();
Object.entries(object)
.map(([key, value]) => {
if (value && typeof value === "object") return [key, deepSortedEntries(value)];
return [key, value];
})
.sort();
var mfn = memoize(
function(hash) {
// body of memoized function
},
{
normalizer: function(args) {
// args is arguments object as accessible in memoized function
return JSON.stringify(deepSortedEntries(args[0]));
}
}
function (hash) {
// body of memoized function
},
{
normalizer: function (args) {
// args is arguments object as accessible in memoized function
return JSON.stringify(deepSortedEntries(args[0]));
},
}
);
mfn({ foo: "bar", bar: "foo" });
@@ -174,14 +172,7 @@ memoized = memoize(fn, { length: 2, resolvers: [String, Boolean] });
memoized(12, [1, 2, 3].length);
memoized("12", true); // Cache hit
memoized(
{
toString: function() {
return "12";
}
},
{}
); // Cache hit
memoized({ toString: function () { return "12"; } }, {}); // Cache hit
```
**Note. If your arguments are collections (arrays or hashes) that you want to memoize by content (not by self objects), you need to cast them to strings**, for it's best to just use [primitive mode](#primitive-mode). Arrays have standard string representation and work with primitive mode out of a box, for hashes you need to define `toString` method, that will produce unique string descriptions, or rely on `JSON.stringify`.
@@ -198,10 +189,8 @@ The difference from natural behavior is that in case when promise was rejected w
the result is immediately removed from memoize cache, and not kept as further reusable result.
```javascript
var afn = function(a, b) {
return new Promise(function(res) {
res(a + b);
});
var afn = function (a, b) {
return new Promise(function (res) { res(a + b); });
};
memoized = memoize(afn, { promise: true });
@@ -220,13 +209,13 @@ memoized = memoize(afn, { promise: "done:finally" });
Supported modes
* `then` _(default)_. Values are resolved purely by passing callbacks to `promise.then`. **Side effect is that eventual unhandled rejection on given promise
come with no logged warning!**, and that to avoid implied error swallowing both states are resolved tick after callbacks were invoked
- `then` _(default)_. Values are resolved purely by passing callbacks to `promise.then`. **Side effect is that eventual unhandled rejection on given promise
come with no logged warning!**, and that to avoid implied error swallowing both states are resolved tick after callbacks were invoked
* `done` Values are resolved purely by passing callback to `done` method. **Side effect is that eventual unhandled rejection on given promise come with no logged warning!**.
- `done` Values are resolved purely by passing callback to `done` method. **Side effect is that eventual unhandled rejection on given promise come with no logged warning!**.
* `done:finally` The only method that may work with no side-effects assuming that promise implementaion does not throw unconditionally
if no _onFailure_ callback was passed to `done`, and promise error was handled by other consumer (this is not commonly implemented _done_ behavior). Otherwise side-effect is that exception is thrown on promise rejection (highly not recommended)
- `done:finally` The only method that may work with no side-effects assuming that promise implementaion does not throw unconditionally
if no _onFailure_ callback was passed to `done`, and promise error was handled by other consumer (this is not commonly implemented _done_ behavior). Otherwise side-effect is that exception is thrown on promise rejection (highly not recommended)
##### Node.js callback style functions
@@ -234,21 +223,19 @@ With _async_ option we indicate that we memoize asynchronous (Node.js style) fun
Operations that result with an error are not cached.
```javascript
afn = function(a, b, cb) {
setTimeout(function() {
cb(null, a + b);
}, 200);
afn = function (a, b, cb) {
setTimeout(function () { cb(null, a + b); }, 200);
};
memoized = memoize(afn, { async: true });
memoized(3, 7, function(err, res) {
memoized(3, 7, function(err, res) {
// Cache hit
});
memoized(3, 7, function (err, res) {
memoized(3, 7, function (err, res) {
// Cache hit
});
});
memoized(3, 7, function(err, res) {
// Cache hit
memoized(3, 7, function (err, res) {
// Cache hit
});
```
@@ -257,12 +244,12 @@ memoized(3, 7, function(err, res) {
When we are defining a prototype, we may want to define a method that will memoize it's results in relation to each instance. A basic way to obtain that would be:
```javascript
var Foo = function() {
this.bar = memoize(this.bar.bind(this), { someOption: true });
// ... constructor logic
var Foo = function () {
this.bar = memoize(this.bar.bind(this), { someOption: true });
// ... constructor logic
};
Foo.prototype.bar = function() {
// ... method logic
Foo.prototype.bar = function () {
// ... method logic
};
```
@@ -272,19 +259,19 @@ There's a lazy methods descriptor generator provided:
var d = require("d");
var memoizeMethods = require("memoizee/methods");
var Foo = function() {
// ... constructor logic
var Foo = function () {
// ... constructor logic
};
Object.defineProperties(
Foo.prototype,
memoizeMethods({
bar: d(
function() {
// ... method logic
},
{ someOption: true }
)
})
Foo.prototype,
memoizeMethods({
bar: d(
function () {
// ... method logic
},
{ someOption: true }
),
})
);
```
@@ -298,9 +285,7 @@ It can be combined with other options mentioned across documentation. However du
```javascript
var memoize = require("memoizee/weak");
var memoized = memoize(function(obj) {
return Object.keys(obj);
});
var memoized = memoize(function (obj) { return Object.keys(obj); });
var obj = { foo: true, bar: false };
memoized(obj);
@@ -334,9 +319,9 @@ memoized = memoize(fn, { maxAge: 1000 }); // 1 second
memoized("foo", 3);
memoized("foo", 3); // Cache hit
setTimeout(function() {
memoized("foo", 3); // No longer in cache, re-executed
memoized("foo", 3); // Cache hit
setTimeout(function () {
memoized("foo", 3); // No longer in cache, re-executed
memoized("foo", 3); // Cache hit
}, 2000);
```
@@ -348,16 +333,16 @@ memoized = memoize(fn, { maxAge: 1000, preFetch: true }); // Defaults to 0.33
memoized("foo", 3);
memoized("foo", 3); // Cache hit
setTimeout(function() {
memoized("foo", 3); // Cache hit
setTimeout(function () {
memoized("foo", 3); // Cache hit
}, 500);
setTimeout(function() {
memoized("foo", 3); // Cache hit, silently pre-fetched in next tick
setTimeout(function () {
memoized("foo", 3); // Cache hit, silently pre-fetched in next tick
}, 800);
setTimeout(function() {
memoized("foo", 3); // Cache hit
setTimeout(function () {
memoized("foo", 3); // Cache hit
}, 1300);
```
@@ -369,12 +354,12 @@ memoized = memoize(fn, { maxAge: 1000, preFetch: 0.6 });
memoized("foo", 3);
memoized("foo", 3); // Cache hit
setTimeout(function() {
memoized("foo", 3); // Cache hit, silently pre-fetched in next tick
setTimeout(function () {
memoized("foo", 3); // Cache hit, silently pre-fetched in next tick
}, 500);
setTimeout(function() {
memoized("foo", 3); // Cache hit
setTimeout(function () {
memoized("foo", 3); // Cache hit
}, 1300);
```
@@ -422,11 +407,7 @@ memoized("bar", 7); // Re-executed, Cache cleared for 'lorem', 11
You can register a callback to be called on each value removed from the cache:
```javascript
memoized = memoize(fn, {
dispose: function(value) {
/*…*/
}
});
memoized = memoize(fn, { dispose: function (value) { /*…*/ } });
var foo3 = memoized("foo", 3);
var bar7 = memoized("bar", 7);
@@ -494,10 +475,10 @@ Memoize statistics:
------------------------------------------------------------
```
* _Init_ Initial hits
* _Cache_ Cache hits
* _%Cache_ What's the percentage of cache hits (of all function calls)
* _Source location_ Where in the source code given memoization was initialized
- _Init_ Initial hits
- _Cache_ Cache hits
- _%Cache_ What's the percentage of cache hits (of all function calls)
- _Source location_ Where in the source code given memoization was initialized
### Tests
@@ -525,8 +506,8 @@ To report a security vulnerability, please use the [Tidelift security contact](h
### Contributors
* [@puzrin](https://github.com/puzrin) (Vitaly Puzrin)
* Proposal and help with coining right _pre-fetch_ logic for [_maxAge_](https://github.com/medikoo/memoize#expire-cache-after-given-period-of-time) variant
- [@puzrin](https://github.com/puzrin) (Vitaly Puzrin)
- Proposal and help with coining right _pre-fetch_ logic for [_maxAge_](https://github.com/medikoo/memoize#expire-cache-after-given-period-of-time) variant
[nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/memoizee/branches/master/shields_badge.svg
[nix-build-url]: https://semaphoreci.com/medikoo-org/memoizee

View File

@@ -31,9 +31,7 @@ var now = Date.now
, lruObj;
getFib = function (memoize, opts) {
var fib = memoize(function (x) {
return x < 2 ? 1 : fib(x - 1) + fib(x - 2);
}, opts);
var fib = memoize(function (x) { return x < 2 ? 1 : fib(x - 1) + fib(x - 2); }, opts);
return fib;
};
@@ -134,7 +132,5 @@ forEach(
console.log(currentIndex + 1 + ":", pad.call(value, " ", 5) + "ms ", name);
},
null,
function (a, b) {
return this[a] - this[b];
}
function (a, b) { return this[a] - this[b]; }
);

View File

@@ -16,7 +16,7 @@ module.exports = {
"type-empty": [2, "never"],
"type-enum": [
2, "always",
["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"]
]
}
["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"],
],
},
};

View File

@@ -28,7 +28,7 @@ require("../lib/registered-extensions").async = function (tbi, conf) {
currentCallback = last;
args = slice.call(args, 0, -1);
}
return base.apply(currentContext = this, currentArgs = args);
return base.apply((currentContext = this), (currentArgs = args));
}, base);
try { mixin(conf.memoized, base); }
catch (ignore) {}

View File

@@ -5,29 +5,23 @@
var callable = require("es5-ext/object/valid-callable")
, forEach = require("es5-ext/object/for-each")
, extensions = require("../lib/registered-extensions")
, apply = Function.prototype.apply;
, apply = Function.prototype.apply;
extensions.dispose = function (dispose, conf, options) {
var del;
callable(dispose);
if ((options.async && extensions.async) || (options.promise && extensions.promise)) {
conf.on("deleteasync", del = function (id, resultArray) {
apply.call(dispose, null, resultArray);
});
conf.on(
"deleteasync",
(del = function (id, resultArray) { apply.call(dispose, null, resultArray); })
);
conf.on("clearasync", function (cache) {
forEach(cache, function (result, id) {
del(id, result);
});
forEach(cache, function (result, id) { del(id, result); });
});
return;
}
conf.on("delete", del = function (id, result) {
dispose(result);
});
conf.on("delete", (del = function (id, result) { dispose(result); }));
conf.on("clear", function (cache) {
forEach(cache, function (result, id) {
del(id, result);
});
forEach(cache, function (result, id) { del(id, result); });
});
};

View File

@@ -13,14 +13,19 @@ extensions.max = function (max, conf, options) {
if (!max) return;
queue = lruQueue(max);
postfix = (options.async && extensions.async) || (options.promise && extensions.promise)
? "async" : "";
postfix =
(options.async && extensions.async) || (options.promise && extensions.promise)
? "async"
: "";
conf.on("set" + postfix, hit = function (id) {
id = queue.hit(id);
if (id === undefined) return;
conf.delete(id);
});
conf.on(
"set" + postfix,
(hit = function (id) {
id = queue.hit(id);
if (id === undefined) return;
conf.delete(id);
})
);
conf.on("get" + postfix, hit);
conf.on("delete" + postfix, queue.delete);
conf.on("clear" + postfix, queue.clear);

View File

@@ -2,30 +2,24 @@
"use strict";
var d = require("d")
, extensions = require("../lib/registered-extensions")
, create = Object.create, defineProperties = Object.defineProperties;
var d = require("d")
, extensions = require("../lib/registered-extensions")
, create = Object.create
, defineProperties = Object.defineProperties;
extensions.refCounter = function (ignore, conf, options) {
var cache, postfix;
cache = create(null);
postfix = (options.async && extensions.async) || (options.promise && extensions.promise)
? "async" : "";
postfix =
(options.async && extensions.async) || (options.promise && extensions.promise)
? "async"
: "";
conf.on("set" + postfix, function (id, length) {
cache[id] = length || 1;
});
conf.on("get" + postfix, function (id) {
++cache[id];
});
conf.on("delete" + postfix, function (id) {
delete cache[id];
});
conf.on("clear" + postfix, function () {
cache = {};
});
conf.on("set" + postfix, function (id, length) { cache[id] = length || 1; });
conf.on("get" + postfix, function (id) { ++cache[id]; });
conf.on("delete" + postfix, function (id) { delete cache[id]; });
conf.on("clear" + postfix, function () { cache = {}; });
defineProperties(conf.memoized, {
deleteRef: d(function () {
@@ -43,6 +37,6 @@ extensions.refCounter = function (ignore, conf, options) {
if (id === null) return 0;
if (!cache[id]) return 0;
return cache[id];
})
}),
});
};

View File

@@ -17,8 +17,8 @@ module.exports = function (fn/*, options*/) {
options.normalizer = require("./normalizers/get-primitive-fixed")(length);
}
} else if (length === false) options.normalizer = require("./normalizers/get")();
else if (length === 1) options.normalizer = require("./normalizers/get-1")();
else options.normalizer = require("./normalizers/get-fixed")(length);
else if (length === 1) options.normalizer = require("./normalizers/get-1")();
else options.normalizer = require("./normalizers/get-fixed")(length);
}
}

View File

@@ -136,7 +136,7 @@ module.exports = function (original, length, options) {
return on.call(this, type, listener);
},
emit: emit,
updateEnv: function () { original = conf.original; }
updateEnv: function () { original = conf.original; },
};
if (get) {
extDel = defineLength(function (arg) {
@@ -176,7 +176,7 @@ module.exports = function (original, length, options) {
delete: d(extDel),
clear: d(conf.clear),
_get: d(extGet),
_has: d(extHas)
_has: d(extHas),
});
return conf;
};

View File

@@ -16,9 +16,7 @@ module.exports = function (memoize) {
options = normalizeOpts(options);
if (length === undefined) {
length = resolveLength(
options.length,
fn.length,
options.async && extensions.async
options.length, fn.length, options.async && extensions.async
);
}
options.normalizer = options.getNormalizer(length);

View File

@@ -7,15 +7,13 @@ var toArray = require("es5-ext/array/to-array")
var slice = Array.prototype.slice, resolveArgs;
resolveArgs = function (args) {
return this.map(function (resolve, i) {
return resolve ? resolve(args[i]) : args[i];
}).concat(slice.call(args, this.length));
return this.map(function (resolve, i) { return resolve ? resolve(args[i]) : args[i]; }).concat(
slice.call(args, this.length)
);
};
module.exports = function (resolvers) {
resolvers = toArray(resolvers);
resolvers.forEach(function (resolve) {
if (isValue(resolve)) callable(resolve);
});
resolvers.forEach(function (resolve) { if (isValue(resolve)) callable(resolve); });
return resolveArgs.bind(resolvers);
};

View File

@@ -58,7 +58,7 @@ module.exports = function (memoize) {
delete: d(function (obj) {
if (resolve) obj = resolve(arguments)[0];
return map.delete(obj);
})
}),
}
);
}
@@ -82,7 +82,7 @@ module.exports = function (memoize) {
);
}
}
map.set(obj, memoizer = memoize(partial.call(fn, obj), options));
map.set(obj, (memoizer = memoize(partial.call(fn, obj), options)));
}
return memoizer.apply(this, slice.call(args, 1));
}, length),
@@ -99,7 +99,7 @@ module.exports = function (memoize) {
if (!memoizer) return;
memoizer.delete.apply(this, slice.call(args, 1));
}, length)
)
),
}
);
if (!options.refCounter) return memoized;
@@ -127,7 +127,7 @@ module.exports = function (memoize) {
if (!memoizer) return 0;
return memoizer.getRefCount.apply(this, slice.call(args, 1));
}, length)
)
),
});
return memoized;
};

View File

@@ -24,6 +24,6 @@ module.exports = function () {
clear: function () {
argsMap = [];
cache = [];
}
},
};
};

View File

@@ -66,6 +66,6 @@ module.exports = function (length) {
clear: function () {
map = [[], []];
cache = create(null);
}
},
};
};

View File

@@ -2,9 +2,7 @@
module.exports = function (length) {
if (!length) {
return function () {
return "";
};
return function () { return ""; };
}
return function (args) {
var id = String(args[0]), i = 0, currentLength = length;

View File

@@ -85,6 +85,6 @@ module.exports = function () {
clear: function () {
map = [];
cache = create(null);
}
},
};
};

View File

@@ -3,7 +3,7 @@
module.exports = function (args) {
var id, i, length = args.length;
if (!length) return "\u0002";
id = String(args[i = 0]);
id = String(args[(i = 0)]);
while (--length) id += "\u0001" + args[++i];
return id;
};

View File

@@ -1,80 +1,80 @@
{
"name": "memoizee",
"version": "0.4.15",
"description": "Memoize/cache function results",
"author": "Mariusz Nowak <medikoo@medikoo.com> (http://www.medikoo.com/)",
"keywords": [
"memoize",
"memoizer",
"cache",
"memoization",
"memo",
"memcached",
"hashing.",
"storage",
"caching",
"memory",
"gc",
"weak",
"garbage",
"collector",
"async"
],
"repository": "medikoo/memoizee",
"dependencies": {
"d": "^1.0.1",
"es5-ext": "^0.10.53",
"es6-weak-map": "^2.0.3",
"event-emitter": "^0.3.5",
"is-promise": "^2.2.2",
"lru-queue": "^0.1.0",
"next-tick": "^1.1.0",
"timers-ext": "^0.1.7"
},
"devDependencies": {
"bluebird": "^3.7.2",
"eslint": "^8.57.0",
"eslint-config-medikoo": "^4.2.0",
"plain-promise": "^0.1.1",
"prettier-elastic": "^3.2.5",
"tad": "^3.1.1"
},
"eslintConfig": {
"extends": "medikoo/es5",
"root": true,
"globals": {
"setTimeout": true,
"clearTimeout": true
},
"rules": {
"max-lines-per-function": "off"
}
},
"prettier": {
"printWidth": 100,
"tabWidth": 4,
"overrides": [
{
"files": [
"*.md",
"*.yml"
],
"options": {
"tabWidth": 2
}
}
]
},
"scripts": {
"lint": "eslint --ignore-path=.gitignore .",
"prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
"name": "memoizee",
"version": "0.4.15",
"description": "Memoize/cache function results",
"author": "Mariusz Nowak <medikoo@medikoo.com> (http://www.medikoo.com/)",
"keywords": [
"memoize",
"memoizer",
"cache",
"memoization",
"memo",
"memcached",
"hashing.",
"storage",
"caching",
"memory",
"gc",
"weak",
"garbage",
"collector",
"async"
],
"repository": "medikoo/memoizee",
"dependencies": {
"d": "^1.0.1",
"es5-ext": "^0.10.53",
"es6-weak-map": "^2.0.3",
"event-emitter": "^0.3.5",
"is-promise": "^2.2.2",
"lru-queue": "^0.1.0",
"next-tick": "^1.1.0",
"timers-ext": "^0.1.7"
},
"devDependencies": {
"bluebird": "^3.7.2",
"eslint": "^8.57.0",
"eslint-config-medikoo": "^4.2.0",
"plain-promise": "^0.1.1",
"prettier-elastic": "^3.2.5",
"tad": "^3.1.1"
},
"eslintConfig": {
"extends": "medikoo/es5",
"root": true,
"globals": {
"setTimeout": true,
"clearTimeout": true
},
"rules": {
"max-lines-per-function": "off"
}
},
"prettier": {
"printWidth": 100,
"tabWidth": 4,
"overrides": [
{
"files": [
"*.md",
"*.yml"
],
"options": {
"tabWidth": 2
}
}
]
},
"scripts": {
"lint": "eslint --ignore-path=.gitignore .",
"prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
"prettier-check:updated": "pipe-git-updated --base=main --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c",
"prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
"prettify:updated": "pipe-git-updated ---base=main -ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write",
"test": "tad"
},
"engines": {
"node": ">=.0.12"
},
"license": "ISC"
},
"engines": {
"node": ">=.0.12"
},
"license": "ISC"
}

View File

@@ -6,7 +6,7 @@ var callable = require("es5-ext/object/valid-callable")
, configure = require("./lib/configure-map")
, resolveLength = require("./lib/resolve-length");
module.exports = function self(fn /*, options */) {
module.exports = function self(fn/*, options */) {
var options, length, conf;
callable(fn);

View File

@@ -9,23 +9,25 @@ var partial = require("es5-ext/function/#/partial")
, d = require("d")
, memoize = require("./plain");
var max = Math.max, stats = exports.statistics = {};
var max = Math.max, stats = (exports.statistics = {});
Object.defineProperty(
memoize,
"__profiler__",
memoize, "__profiler__",
d(function (conf) {
var id, source, data, stack;
stack = new Error().stack;
if (
!stack ||
!stack.split("\n").slice(3).some(function (line) {
if (line.indexOf("/memoizee/") === -1 && line.indexOf(" (native)") === -1) {
source = line.replace(/\n/g, "\\n").trim();
return true;
}
return false;
})
!stack
.split("\n")
.slice(3)
.some(function (line) {
if (line.indexOf("/memoizee/") === -1 && line.indexOf(" (native)") === -1) {
source = line.replace(/\n/g, "\\n").trim();
return true;
}
return false;
})
) {
source = "unknown";
}

View File

@@ -2,5 +2,5 @@
---
version: 1.0.0
codeOwners:
- '0x641A74E8f6b1f59ffadc28b220A316c79a65D87e'
- "0x641A74E8f6b1f59ffadc28b220A316c79a65D87e"
quorum: 1

View File

@@ -25,40 +25,35 @@ module.exports = function () {
++invoked;
a.deep([err, res], [null, 10], "Result #1");
}),
u,
"Initial"
u, "Initial"
);
a(
mfn(3, 7, function (err, res) {
++invoked;
a.deep([err, res], [null, 10], "Result #2");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
++invoked;
a.deep([err, res], [null, 13], "Result B #1");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(3, 7, function (err, res) {
++invoked;
a.deep([err, res], [null, 10], "Result #3");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
++invoked;
a.deep([err, res], [null, 13], "Result B #2");
}),
u,
"Initial #3"
u, "Initial #3"
);
nextTick(function () {
@@ -70,16 +65,14 @@ module.exports = function () {
++invoked;
a.deep([err, res], [null, 10], "Again: Result");
}),
u,
"Again: Initial"
u, "Again: Initial"
);
a(
mfn(5, 8, function (err, res) {
++invoked;
a.deep([err, res], [null, 13], "Again B: Result");
}),
u,
"Again B: Initial"
u, "Again B: Initial"
);
nextTick(function () {
@@ -93,16 +86,14 @@ module.exports = function () {
++invoked;
a.deep([err, res], [null, 10], "Again: Result");
}),
u,
"Again: Initial"
u, "Again: Initial"
);
a(
mfn(5, 8, function (err, res) {
++invoked;
a.deep([err, res], [null, 13], "Again B: Result");
}),
u,
"Again B: Initial"
u, "Again B: Initial"
);
nextTick(function () {
@@ -129,36 +120,31 @@ module.exports = function () {
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Result #1");
}),
u,
"Initial"
u, "Initial"
);
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Result #2");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [e, undefined], "Result B #1");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Result #3");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [e, undefined], "Result B #2");
}),
u,
"Initial #3"
u, "Initial #3"
);
nextTick(function () {
@@ -168,15 +154,13 @@ module.exports = function () {
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Again: Result");
}),
u,
"Again: Initial"
u, "Again: Initial"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [e, undefined], "Again B: Result");
}),
u,
"Again B: Initial"
u, "Again B: Initial"
);
nextTick(function () {
@@ -184,7 +168,7 @@ module.exports = function () {
d();
});
});
}
},
},
"Primitive": {
"Success": function (a, d) {
@@ -200,39 +184,28 @@ module.exports = function () {
mfn = memoize(fn, { async: true, primitive: true });
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [null, 10], "Result #1");
}),
u,
"Initial"
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }),
u, "Initial"
);
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [null, 10], "Result #2");
}),
u,
"Initial #2"
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }),
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [null, 13], "Result B #1");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [null, 10], "Result #3");
}),
u,
"Initial #2"
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }),
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [null, 13], "Result B #2");
}),
u,
"Initial #3"
u, "Initial #3"
);
nextTick(function () {
@@ -242,15 +215,13 @@ module.exports = function () {
mfn(3, 7, function (err, res) {
a.deep([err, res], [null, 10], "Again: Result");
}),
u,
"Again: Initial"
u, "Again: Initial"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [null, 13], "Again B: Result");
}),
u,
"Again B: Initial"
u, "Again B: Initial"
);
nextTick(function () {
@@ -262,15 +233,13 @@ module.exports = function () {
mfn(3, 7, function (err, res) {
a.deep([err, res], [null, 10], "Again: Result");
}),
u,
"Again: Initial"
u, "Again: Initial"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [null, 13], "Again B: Result");
}),
u,
"Again B: Initial"
u, "Again B: Initial"
);
nextTick(function () {
@@ -296,36 +265,31 @@ module.exports = function () {
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Result #1");
}),
u,
"Initial"
u, "Initial"
);
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Result #2");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [e, undefined], "Result B #1");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Result #3");
}),
u,
"Initial #2"
u, "Initial #2"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [e, undefined], "Result B #2");
}),
u,
"Initial #3"
u, "Initial #3"
);
nextTick(function () {
@@ -335,15 +299,13 @@ module.exports = function () {
mfn(3, 7, function (err, res) {
a.deep([err, res], [e, undefined], "Again: Result");
}),
u,
"Again: Initial"
u, "Again: Initial"
);
a(
mfn(5, 8, function (err, res) {
a.deep([err, res], [e, undefined], "Again B: Result");
}),
u,
"Again B: Initial"
u, "Again B: Initial"
);
nextTick(function () {
@@ -354,34 +316,25 @@ module.exports = function () {
},
"Primitive null arg case": function (a, d) {
var x = {}
, mfn = memoize(
function f(id, cb) {
cb(null, x);
},
{
, mfn = memoize(function f(id, cb) { cb(null, x); }, {
async: true,
primitive: true
}
);
primitive: true,
});
mfn(null, function (err, res) {
a.deep([err, res], [null, x], "Args");
d();
});
}
},
},
"Sync Clear": function (a, d) {
var mfn, fn;
fn = function (x, cb) {
nextTick(function () {
cb(null, x);
});
nextTick(function () { cb(null, x); });
};
mfn = memoize(fn, { async: true });
mfn(1, function (err, i) {
a(i, 1, "First");
});
mfn(1, function (err, i) { a(i, 1, "First"); });
mfn.clear();
mfn(2, function (err, i) {
a(i, 2, "Second");
@@ -391,23 +344,17 @@ module.exports = function () {
"Sync Clear: Primitive": function (a, d) {
var mfn, fn;
fn = function (x, cb) {
nextTick(function () {
cb(null, x);
});
nextTick(function () { cb(null, x); });
};
mfn = memoize(fn, { async: true, primitive: true });
mfn(2, function (err, i) {
a(i, 2, "First");
});
mfn(2, function (err, i) { a(i, 2, "First"); });
mfn(1, function (err, i) {
a(i, 1, "Second");
nextTick(d);
});
mfn.clear();
mfn(2, function (err, i) {
a(i, 2, "Third");
});
}
mfn(2, function (err, i) { a(i, 2, "Third"); });
},
};
};

View File

@@ -32,7 +32,7 @@ module.exports = function () {
x = {};
invoked = false;
mfn = memoize(function () { return x; }, {
dispose: function (val) { invoked = val; }
dispose: function (val) { invoked = val; },
});
mfn.delete();
@@ -47,7 +47,7 @@ module.exports = function () {
fn = function (x, y) { return x + y; };
mfn = memoize(fn, {
refCounter: true,
dispose: function (val) { value.push(val); }
dispose: function (val) { value.push(val); },
});
mfn(3, 7);
@@ -128,7 +128,7 @@ module.exports = function () {
);
});
});
}
},
},
Primitive: {
"Sync": function (a) {
@@ -156,7 +156,7 @@ module.exports = function () {
fn = function (x, y) { return x + y; };
mfn = memoize(fn, {
refCounter: true,
dispose: function (val) { value.push(val); }
dispose: function (val) { value.push(val); },
});
mfn(3, 7);
@@ -237,7 +237,7 @@ module.exports = function () {
);
});
});
}
}
},
},
};
};

View File

@@ -169,7 +169,7 @@ module.exports = function () {
});
}, 100);
}, 20);
}
},
},
Primitive: {
Sync: function (a, d) {
@@ -324,7 +324,7 @@ module.exports = function () {
});
}, 500);
}, 100);
}
},
},
Refetch: {
Default: function (a, d) {
@@ -450,116 +450,122 @@ module.exports = function () {
i, 4,
"Called: After Refetch: Before"
);
mfn(3, 7, function (
err,
result
) {
a(
result, 10,
"Result: After Refetch"
);
a(
i, 4,
"Called: After Refetch: After"
);
mfn(5, 8, function (
err,
result
) {
mfn(
3,
7,
function (err, result) {
a(
result, 13,
"Result: After Refetch B"
result, 10,
"Result: After Refetch"
);
a(
i, 4,
"Called: After Refetch B: After"
"Called: After Refetch: After"
);
// Wait 250ms
setTimeout(
function () {
// From cache, prefetch triggered
mfn(
3,
7,
function (
err,
result
) {
a(
result,
10,
"Result: After Refetch #2"
);
a(
i,
4,
"Called: After Refetch #2"
);
mfn(
5,
8,
function (
err,
result
) {
a(
result,
13,
"Result: After Refetch B"
);
a(
i, 4,
"Called: After Refetch B: After"
);
// Wait 250ms
setTimeout(
function () {
// From cache, prefetch triggered
mfn(
5,
8,
3,
7,
function (
err,
result
) {
a(
result,
13,
"Result: After Refetch #2 B"
10,
"Result: After Refetch #2"
);
a(
i,
5,
"Called: After Refetch #2 B"
4,
"Called: After Refetch #2"
);
mfn(
3,
7,
5,
8,
function (
err,
result
) {
a(
result,
10,
"Result: After Refetch #3"
13,
"Result: After Refetch #2 B"
);
a(
i,
6,
"Called: After Refetch #3"
5,
"Called: After Refetch #2 B"
);
mfn(
5,
8,
3,
7,
function (
err,
result
) {
a(
result,
13,
"Result: After Refetch #3 B"
10,
"Result: After Refetch #3"
);
a(
i,
6,
"Called: After Refetch #3 B"
"Called: After Refetch #3"
);
mfn(
5,
8,
function (
err,
result
) {
a(
result,
13,
"Result: After Refetch #3 B"
);
a(
i,
6,
"Called: After Refetch #3 B"
);
d();
}
);
d();
}
);
}
);
}
);
}
},
250
);
},
250
}
);
});
});
}
);
}, 200);
});
});
@@ -856,7 +862,7 @@ module.exports = function () {
}, 3000);
}, 1000);
}, 4500);
}
}
},
},
};
};

File diff suppressed because it is too large Load Diff

View File

@@ -8,9 +8,7 @@ var memoize = require("../..")
, Promise = require("plain-promise")
, Bluebird = require("bluebird");
Bluebird.config({
cancellation: true
});
Bluebird.config({ cancellation: true });
module.exports = function () {
return {
@@ -114,7 +112,7 @@ module.exports = function () {
d();
}, 10);
}, 10);
}
},
},
"Cancellation": {
Immediate: function (a, d) {
@@ -122,9 +120,7 @@ module.exports = function () {
fn = function (x, y) {
++i;
var p = new Bluebird(function (res) {
setTimeout(function () {
res(x + y);
}, 100);
setTimeout(function () { res(x + y); }, 100);
});
p.cancel();
return p;
@@ -133,30 +129,27 @@ module.exports = function () {
mfn = memoize(fn, { promise: true });
mfn(3, 7).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Result #1");
a.throws(function () { throw err; }, Bluebird.CancellationError, "Result #1");
});
mfn(5, 8).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Result B #2");
a.throws(function () { throw err; }, Bluebird.CancellationError, "Result B #2");
});
setTimeout(function () {
a(i, 2, "Called #2");
mfn(3, 7).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Again: Result");
a.throws(
function () { throw err; }, Bluebird.CancellationError, "Again: Result"
);
});
mfn(5, 8).done(a.never, function (err) {
a.throws(function () {
throw err;
}, Bluebird.CancellationError, "Again B: Result");
a.throws(
function () { throw err; }, Bluebird.CancellationError,
"Again B: Result"
);
});
setTimeout(function (err) {
@@ -170,13 +163,9 @@ module.exports = function () {
fn = function (x, y) {
++i;
var p = new Bluebird(function (res) {
setTimeout(function () {
res(x + y);
}, 100);
setTimeout(function () { res(x + y); }, 100);
});
nextTick(function () {
p.cancel();
}, 1);
nextTick(function () { p.cancel(); }, 1);
return p;
};
@@ -198,7 +187,7 @@ module.exports = function () {
d();
}, 500);
}, 500);
}
},
},
"Primitive": {
"Success": function (a, d) {
@@ -286,7 +275,7 @@ module.exports = function () {
a.deep(res, x, "Args");
d();
}, a.never);
}
},
},
"Sync Clear": function (a, d) {
var mfn, fn;
@@ -321,6 +310,6 @@ module.exports = function () {
a(res, 2, "Second");
d();
}, a.never);
}
},
};
};

View File

@@ -297,6 +297,6 @@ module.exports = function () {
}, 10);
}, 10);
}, 10);
}
},
};
};

File diff suppressed because it is too large Load Diff

View File

@@ -10,15 +10,11 @@ module.exports = function () {
"One arg": function (a) {
var i = 0
, fn = function (x) {
++i;
return x;
}
, mfn
, y = {
toString: function () {
return "foo";
++i;
return x;
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = memoize(fn, { primitive: true });
a(mfn(y), y, "#1");
a(mfn("foo"), y, "#2");
@@ -27,15 +23,11 @@ module.exports = function () {
"No args": function (a) {
var i = 0
, fn = function () {
++i;
return "foo";
}
, mfn
, y = {
toString: function () {
++i;
return "foo";
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = memoize(fn);
a(mfn._has(), false);
a(mfn(), "foo", "#1");
@@ -46,46 +38,28 @@ module.exports = function () {
"Clear cache": function (a) {
var i = 0
, fn = function (x, y, z) {
++i;
return x + y + z;
}
, mfn
, y = {
toString: function () {
return "foo";
++i;
return x + y + z;
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = memoize(fn, { primitive: true });
a(mfn(y, "bar", "zeta"), "foobarzeta", "#1");
a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2");
a(i, 1, "Called once");
mfn.delete(
"foo",
{
toString: function () {
return "bar";
}
},
"zeta"
);
mfn.delete("foo", { toString: function () { return "bar"; } }, "zeta");
a(mfn(y, "bar", "zeta"), "foobarzeta", "#3");
a(i, 2, "Called twice");
},
"_get": function (a) {
var fn = function (x) {
return x;
}
, mfn;
var fn = function (x) { return x; }, mfn;
mfn = memoize(fn);
a(mfn._get("foo"), undefined);
mfn("foo");
a(mfn._get("foo"), "foo");
},
"_has": function (a) {
var fn = function (x) {
return x;
}
, mfn;
var fn = function (x) { return x; }, mfn;
mfn = memoize(fn);
a(mfn._has("foo"), false);
mfn("foo");
@@ -93,20 +67,12 @@ module.exports = function () {
},
"Circular": function (a) {
var i = 0, fn;
fn = memoize(function (x) {
if (++i < 2) fn(x);
});
a.throws(function () {
fn("foo");
}, "CIRCULAR_INVOCATION");
fn = memoize(function (x) { if (++i < 2) fn(x); });
a.throws(function () { fn("foo"); }, "CIRCULAR_INVOCATION");
i = 0;
fn = memoize(function (x, y) {
if (++i < 2) fn(x, y);
});
a.throws(function () {
fn("foo", "bar");
}, "CIRCULAR_INVOCATION");
fn = memoize(function (x, y) { if (++i < 2) fn(x, y); });
a.throws(function () { fn("foo", "bar"); }, "CIRCULAR_INVOCATION");
},
"Resolvers": function () {
var i = 0, fn, r;
@@ -120,7 +86,7 @@ module.exports = function () {
return {
"No args": function (a) {
i = 0;
a.deep(aFrom(r = fn()), [false, "undefined"], "First");
a.deep(aFrom((r = fn())), [false, "undefined"], "First");
a(fn(), r, "Second");
a(fn(), r, "Third");
a(i, 1, "Called once");
@@ -133,24 +99,24 @@ module.exports = function () {
},
{ resolvers: [Boolean] }
);
a.deep(aFrom(r = fn("elo")), [true], "First");
a.deep(aFrom((r = fn("elo"))), [true], "First");
},
"Some Args": function (a) {
var x = {};
i = 0;
a.deep(aFrom(r = fn(0, 34, x, 45)), [false, "34", x, 45], "First");
a.deep(aFrom((r = fn(0, 34, x, 45))), [false, "34", x, 45], "First");
a(fn(0, 34, x, 22), r, "Second");
a(fn(0, 34, x, false), r, "Third");
a(i, 1, "Called once");
return {
Other: function (a) {
a.deep(aFrom(r = fn(1, 34, x, 34)), [true, "34", x, 34], "Second");
a.deep(aFrom((r = fn(1, 34, x, 34))), [true, "34", x, 34], "Second");
a(fn(1, 34, x, 89), r, "Third");
a(i, 2, "Called once");
}
},
};
}
},
};
}
},
};
};

View File

@@ -19,13 +19,8 @@ module.exports = function (t, a) {
a(this, obj);
return x + y;
},
{
refCounter: true,
dispose: function (val) {
value.push(val);
}
}
)
{ refCounter: true, dispose: function (val) { value.push(val); } }
),
})
);

View File

@@ -15,12 +15,7 @@ module.exports = function (t, a) {
a(arg, obj);
return x + y;
},
{
refCounter: true,
dispose: function (val) {
value.push(val);
}
}
{ refCounter: true, dispose: function (val) { value.push(val); } }
);
a(memoized(obj, 3, 7), 10);
@@ -42,9 +37,7 @@ module.exports = function (t, a) {
x = {};
y = {};
z = {};
memoized = t(function (arg) {
return ++count;
});
memoized = t(function (arg) { return ++count; });
a(memoized(x), 1);
a(memoized(y), 2);
a(memoized(x), 1);

View File

@@ -17,13 +17,8 @@ module.exports = function (t, a) {
a(this, obj);
return x + y;
},
{
refCounter: true,
dispose: function (val) {
value.push(val);
}
}
)
{ refCounter: true, dispose: function (val) { value.push(val); } }
),
})
);

View File

@@ -14,13 +14,8 @@ module.exports = function (t, a) {
a(this, obj);
return x + y;
},
{
refCounter: true,
dispose: function (val) {
value.push(val);
}
}
)
{ refCounter: true, dispose: function (val) { value.push(val); } }
),
})
);

View File

@@ -8,9 +8,9 @@ module.exports = {
"": function (t, a) {
var i = 0
, fn = function (x) {
++i;
return x;
};
++i;
return x;
};
fn = memoize(fn);
return {
@@ -36,15 +36,13 @@ module.exports = {
a(fn(x, 9), x, "Second");
a(fn(x, 3), x, "Third");
a(i, 1, "Called once");
}
},
};
},
"Delete": function (a) {
var i = 0, fn, mfn, x = {};
fn = function (a, b, c) {
return a + ++i;
};
fn = function (a, b, c) { return a + ++i; };
mfn = memoize(fn, { length: 1 });
a(mfn(3), 4, "Init");
a(mfn(4, x, 1), 6, "Init #2");
@@ -58,5 +56,5 @@ module.exports = {
a(i, 3, "Reinit count");
a(mfn(3, x, 1), 6, "Reinit Cached");
a(i, 3, "Reinit count");
}
},
};

View File

@@ -8,16 +8,16 @@ module.exports = {
"": function (a) {
var i = 0
, fn = function (x, y, z) {
++i;
return [x, y, z];
}
++i;
return [x, y, z];
}
, r;
fn = memoize(fn);
return {
"No args": function () {
i = 0;
a.deep(r = fn(), [undefined, undefined, undefined], "First");
a.deep((r = fn()), [undefined, undefined, undefined], "First");
a(fn(), r, "Second");
a(fn(), r, "Third");
a(i, 1, "Called once");
@@ -25,41 +25,39 @@ module.exports = {
"Some Args": function () {
var x = {};
i = 0;
a.deep(r = fn(x, 8), [x, 8, undefined], "First");
a.deep((r = fn(x, 8)), [x, 8, undefined], "First");
a(fn(x, 8), r, "Second");
a(fn(x, 8), r, "Third");
a(i, 1, "Called once");
return {
Other: function () {
a.deep(r = fn(x, 5), [x, 5, undefined], "Second");
a.deep((r = fn(x, 5)), [x, 5, undefined], "Second");
a(fn(x, 5), r, "Third");
a(i, 2, "Called once");
}
},
};
},
"Full stuff": function () {
var x = {};
i = 0;
a.deep(r = fn(x, 8, 23, 98), [x, 8, 23], "First");
a.deep((r = fn(x, 8, 23, 98)), [x, 8, 23], "First");
a(fn(x, 8, 23, 43), r, "Second");
a(fn(x, 8, 23, 9), r, "Third");
a(i, 1, "Called once");
return {
Other: function () {
a.deep(r = fn(x, 23, 8, 13), [x, 23, 8], "Second");
a.deep((r = fn(x, 23, 8, 13)), [x, 23, 8], "Second");
a(fn(x, 23, 8, 22), r, "Third");
a(i, 2, "Called once");
}
},
};
}
},
};
},
"Delete": function (a) {
var i = 0, fn, mfn, x = {};
fn = function (a, b, c) {
return a + ++i;
};
fn = function (a, b, c) { return a + ++i; };
mfn = memoize(fn);
a(mfn(3, x, 1), 4, "Init");
a(mfn(4, x, 1), 6, "Init #2");
@@ -94,5 +92,5 @@ module.exports = {
fn(1, x, 3);
fn(1, x, 4);
a(i, 4, "After clear");
}
},
};

View File

@@ -8,15 +8,11 @@ module.exports = {
"": function (a) {
var i = 0
, fn = function (x, y, z) {
++i;
return x + y + z;
}
, mfn
, y = {
toString: function () {
return "foo";
++i;
return x + y + z;
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = memoize(fn, { primitive: true });
a(mfn(y, "bar", "zeta"), "foobarzeta", "#1");
a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2");
@@ -25,46 +21,26 @@ module.exports = {
"Delete": function (a) {
var i = 0
, fn = function (x, y, z) {
++i;
return x + y + z;
}
, mfn
, y = {
toString: function () {
return "foo";
++i;
return x + y + z;
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = memoize(fn, { primitive: true });
a(mfn(y, "bar", "zeta"), "foobarzeta", "#1");
a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2");
a(i, 1, "Called once");
mfn.delete(
"foo",
{
toString: function () {
return "bar";
}
},
"zeta"
);
mfn.delete("foo", { toString: function () { return "bar"; } }, "zeta");
a(mfn(y, "bar", "zeta"), "foobarzeta", "#3");
a(i, 2, "Called twice");
},
"Clear": function (a) {
var i = 0, fn;
fn = memoize(function (x) {
if (++i < 2) fn(x);
});
a.throws(function () {
fn("foo");
}, "CIRCULAR_INVOCATION");
fn = memoize(function (x) { if (++i < 2) fn(x); });
a.throws(function () { fn("foo"); }, "CIRCULAR_INVOCATION");
i = 0;
fn = memoize(function (x, y) {
if (++i < 2) fn(x, y);
});
a.throws(function () {
fn("foo", "bar");
}, "CIRCULAR_INVOCATION");
}
fn = memoize(function (x, y) { if (++i < 2) fn(x, y); });
a.throws(function () { fn("foo", "bar"); }, "CIRCULAR_INVOCATION");
},
};

View File

@@ -10,16 +10,16 @@ module.exports = function () {
"": function (a) {
var i = 0
, fn = function () {
++i;
return arguments;
}
++i;
return arguments;
}
, r;
fn = memoize(fn, { length: false });
return {
"No args": function () {
i = 0;
a.deep(aFrom(r = fn()), [], "First");
a.deep(aFrom((r = fn())), [], "First");
a(fn(), r, "Second");
a(fn(), r, "Third");
a(i, 1, "Called once");
@@ -27,7 +27,7 @@ module.exports = function () {
"Some Args": function () {
var x = {};
i = 0;
a.deep(aFrom(r = fn(x, 8)), [x, 8], "First");
a.deep(aFrom((r = fn(x, 8))), [x, 8], "First");
a(fn(x, 8), r, "Second");
a(fn(x, 8), r, "Third");
a(i, 1, "Called once");
@@ -35,19 +35,17 @@ module.exports = function () {
"Many args": function () {
var x = {};
i = 0;
a.deep(aFrom(r = fn(x, 8, 23, 98)), [x, 8, 23, 98], "First");
a.deep(aFrom((r = fn(x, 8, 23, 98))), [x, 8, 23, 98], "First");
a(fn(x, 8, 23, 98), r, "Second");
a(fn(x, 8, 23, 98), r, "Third");
a(i, 1, "Called once");
}
},
};
},
"Delete": function (a) {
var i = 0, fn, mfn, x = {};
fn = function (a, b, c) {
return a + ++i;
};
fn = function (a, b, c) { return a + ++i; };
mfn = memoize(fn, { length: false });
a(mfn(3, x, 1), 4, "Init");
a(mfn(4, x, 1), 6, "Init #2");
@@ -61,6 +59,6 @@ module.exports = function () {
a(i, 3, "Reinit count");
a(mfn(3, x, 1), 6, "Reinit Cached");
a(i, 3, "Reinit count");
}
},
};
};

View File

@@ -8,14 +8,10 @@ var memoize = require("../..")
module.exports = function (a) {
var i = 0
, fn = function () {
++i;
return join.call(arguments, "|");
}
, y = {
toString: function () {
return "foo";
++i;
return join.call(arguments, "|");
}
}
, y = { toString: function () { return "foo"; } }
, mfn;
mfn = memoize(fn, { primitive: true, length: false });
a(mfn(y, "bar", "zeta"), "foo|bar|zeta", "#1");

View File

@@ -7,15 +7,11 @@ module.exports = function (t) {
"": function (a) {
var i = 0
, fn = function (x) {
++i;
return x;
}
, mfn
, y = {
toString: function () {
return "foo";
++i;
return x;
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = t(fn, { primitive: true });
a(typeof mfn, "function", "Returns");
a(mfn.__memoized__, true, "Marked");
@@ -27,30 +23,18 @@ module.exports = function (t) {
"Clear cache": function (a) {
var i = 0
, fn = function (x, y, z) {
++i;
return x + y + z;
}
, mfn
, y = {
toString: function () {
return "foo";
++i;
return x + y + z;
}
};
, mfn
, y = { toString: function () { return "foo"; } };
mfn = t(fn, { primitive: true });
a(mfn(y, "bar", "zeta"), "foobarzeta", "#1");
a(mfn("foo", "bar", "zeta"), "foobarzeta", "#2");
a(i, 1, "Called once");
mfn.delete(
"foo",
{
toString: function () {
return "bar";
}
},
"zeta"
);
mfn.delete("foo", { toString: function () { return "bar"; } }, "zeta");
a(mfn(y, "bar", "zeta"), "foobarzeta", "#3");
a(i, 2, "Called twice");
}
},
};
};

View File

@@ -12,12 +12,7 @@ module.exports = function (t, a) {
a(arg, obj);
return x + y;
},
{
refCounter: true,
dispose: function (val) {
value.push(val);
}
}
{ refCounter: true, dispose: function (val) { value.push(val); } }
);
a(memoized(obj, 3, 7), 10);
@@ -39,9 +34,7 @@ module.exports = function (t, a) {
x = {};
y = {};
z = {};
memoized = t(function (arg) {
return ++count;
});
memoized = t(function (arg) { return ++count; });
a(memoized(x), 1);
a(memoized(y), 2);
a(memoized(x), 1);

View File

@@ -9,12 +9,7 @@ module.exports = function (t, a, d) {
a(arg, obj);
return x + y;
},
{
refCounter: true,
dispose: function (val) {
value.push(val);
}
}
{ refCounter: true, dispose: function (val) { value.push(val); } }
);
a(memoized(obj, 3, 7), 10);
@@ -36,9 +31,7 @@ module.exports = function (t, a, d) {
x = {};
y = {};
z = {};
memoized = t(function (arg) {
return ++count;
});
memoized = t(function (arg) { return ++count; });
a(memoized(x), 1);
a(memoized(y), 2);
a(memoized(x), 1);
@@ -46,12 +39,7 @@ module.exports = function (t, a, d) {
a(count, 3);
count = 0;
memoized = t(
function (arg) {
return ++count;
},
{ maxAge: 1 }
);
memoized = t(function (arg) { return ++count; }, { maxAge: 1 });
memoized(obj);
setTimeout(function () {