fast-memoize
Advanced tools
Comparing version 2.2.0 to 2.2.3
{ | ||
"name": "fast-memoize", | ||
"version": "2.2.0", | ||
"version": "2.2.3", | ||
"description": "Fastest memoization lib that supports N arguments", | ||
@@ -17,2 +17,3 @@ "main": "src/index.js", | ||
"benchmark:v8-optimization-analysis": "node --allow_natives_syntax --expose_debug_as=VirtualMachine benchmark/v8-optimization-analysis.js", | ||
"benchmark:compare": "./benchmark/compare-commits/index.sh", | ||
"lint": "eslint --fix \"src/**/*.js\" \"test/**/*.js\" \"benchmark/**/*.js\"", | ||
@@ -41,2 +42,3 @@ "preversion": "npm run test:all", | ||
"cli-table2": "^0.2.0", | ||
"codecov": "^2.0.2", | ||
"covert": "^1.1.0", | ||
@@ -62,3 +64,4 @@ "eslint": "^3.12.1", | ||
"testRegex": "test/.*\\.js$", | ||
"collectCoverage": true | ||
"collectCoverage": true, | ||
"coverageDirectory": "./coverage/" | ||
}, | ||
@@ -65,0 +68,0 @@ "eslintConfig": { |
@@ -6,3 +6,3 @@ <img src="http://rawgit.com/caiogondim/fast-memoize/master/img/icon.svg" width="100%" /> | ||
<div align="center"> | ||
<img src="http://travis-ci.org/caiogondim/fast-memoize.js.svg?branch=master" alt="Travis CI"> <img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" alt="JS standard style"> | ||
<img src="http://travis-ci.org/caiogondim/fast-memoize.js.svg?branch=master" alt="Travis CI"> <img src="http://img.badgesize.io/caiogondim/fast-memoize.js/master/src/index.js?compression=gzip"> <img src="https://codecov.io/gh/caiogondim/fast-memoize.js/branch/master/graph/badge.svg" alt="Code coverage"> | ||
</div> | ||
@@ -46,3 +46,3 @@ | ||
The fastest cache is used for the running enviroment, but it is possible to | ||
The fastest cache is used for the running environment, but it is possible to | ||
pass a custom cache to be used. | ||
@@ -52,11 +52,19 @@ | ||
const memoized = memoize(fn, { | ||
cache: customCache | ||
cache: { | ||
create() { | ||
var store = {}; | ||
return { | ||
has(key) { return (key in store); }, | ||
get(key) { return store[key]; }, | ||
set(key, value) { store[key] = value; } | ||
}; | ||
} | ||
} | ||
}) | ||
``` | ||
The custom cache must implement the following methods: | ||
The custom cache should be an object containing a `create` method that returns an object implementing the following methods: | ||
- `get` | ||
- `set` | ||
- `has` | ||
- `delete` | ||
@@ -97,2 +105,10 @@ ### Custom serializer | ||
### Against another git hash | ||
To benchmark the current code against a git hash, branch, ... | ||
```shell | ||
npm run benchmark:compare 53fa9a62214e816cf8b5b4fa291c38f1d63677b9 | ||
``` | ||
## Credits | ||
@@ -99,0 +115,0 @@ - Icon by Mary Rankin from the Noun Project |
@@ -6,11 +6,11 @@ // | ||
module.exports = function memoize (fn, options) { | ||
const cache = options && options.cache | ||
var cache = options && options.cache | ||
? options.cache | ||
: cacheDefault | ||
const serializer = options && options.serializer | ||
var serializer = options && options.serializer | ||
? options.serializer | ||
: serializerDefault | ||
const strategy = options && options.strategy | ||
var strategy = options && options.strategy | ||
? options.strategy | ||
@@ -29,32 +29,34 @@ : strategyDefault | ||
const isPrimitive = (value) => | ||
value == null || (typeof value !== 'function' && typeof value !== 'object') | ||
function isPrimitive (value) { | ||
return value == null || (typeof value !== 'function' && typeof value !== 'object') | ||
} | ||
function strategyDefault (fn, options) { | ||
function monadic (fn, cache, serializer, arg) { | ||
const cacheKey = isPrimitive(arg) ? arg : serializer(arg) | ||
function monadic (fn, cache, serializer, arg) { | ||
var cacheKey = isPrimitive(arg) ? arg : serializer(arg) | ||
if (!cache.has(cacheKey)) { | ||
const computedValue = fn.call(this, arg) | ||
cache.set(cacheKey, computedValue) | ||
return computedValue | ||
} | ||
return cache.get(cacheKey) | ||
if (!cache.has(cacheKey)) { | ||
var computedValue = fn.call(this, arg) | ||
cache.set(cacheKey, computedValue) | ||
return computedValue | ||
} | ||
function variadic (fn, cache, serializer, ...args) { | ||
const cacheKey = serializer(args) | ||
return cache.get(cacheKey) | ||
} | ||
if (!cache.has(cacheKey)) { | ||
const computedValue = fn.apply(this, args) | ||
cache.set(cacheKey, computedValue) | ||
return computedValue | ||
} | ||
function variadic (fn, cache, serializer) { | ||
var args = Array.prototype.slice.call(arguments, 3) | ||
var cacheKey = serializer(args) | ||
return cache.get(cacheKey) | ||
if (!cache.has(cacheKey)) { | ||
var computedValue = fn.apply(this, args) | ||
cache.set(cacheKey, computedValue) | ||
return computedValue | ||
} | ||
let memoized = fn.length === 1 ? monadic : variadic | ||
return cache.get(cacheKey) | ||
} | ||
function strategyDefault (fn, options) { | ||
var memoized = fn.length === 1 ? monadic : variadic | ||
memoized = memoized.bind( | ||
@@ -74,3 +76,5 @@ this, | ||
const serializerDefault = (...args) => JSON.stringify(args) | ||
function serializerDefault () { | ||
return JSON.stringify(arguments) | ||
} | ||
@@ -81,18 +85,16 @@ // | ||
class ObjectWithoutPrototypeCache { | ||
constructor () { | ||
this.cache = Object.create(null) | ||
} | ||
function ObjectWithoutPrototypeCache () { | ||
this.cache = Object.create(null) | ||
} | ||
has (key) { | ||
return (key in this.cache) | ||
} | ||
ObjectWithoutPrototypeCache.prototype.has = function (key) { | ||
return (key in this.cache) | ||
} | ||
get (key) { | ||
return this.cache[key] | ||
} | ||
ObjectWithoutPrototypeCache.prototype.get = function (key) { | ||
return this.cache[key] | ||
} | ||
set (key, value) { | ||
this.cache[key] = value | ||
} | ||
ObjectWithoutPrototypeCache.prototype.set = function (key, value) { | ||
this.cache[key] = value | ||
} | ||
@@ -99,0 +101,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8893
77
119
17