micro-memoize
Advanced tools
Comparing version 3.0.0-beta.6 to 3.0.0
@@ -10,2 +10,3 @@ # micro-memoize CHANGELOG | ||
- CommonJS `require`s no longer require `.default` | ||
- Types contract is much stricter | ||
@@ -12,0 +13,0 @@ - Utility methods are no longer deep-linkable |
@@ -22,13 +22,13 @@ 'use strict'; | ||
*/ | ||
function createGetKeyIndex(options) { | ||
if (typeof options.isMatchingKey === 'function') { | ||
var isMatchingKey_1 = options.isMatchingKey, maxSize_1 = options.maxSize; | ||
function createGetKeyIndex(_a) { | ||
var isEqual = _a.isEqual, isMatchingKey = _a.isMatchingKey, maxSize = _a.maxSize; | ||
if (typeof isMatchingKey === 'function') { | ||
return function getKeyIndex(allKeys, keyToMatch) { | ||
if (isMatchingKey_1(allKeys[0], keyToMatch)) { | ||
if (isMatchingKey(allKeys[0], keyToMatch)) { | ||
return 0; | ||
} | ||
if (maxSize_1 > 1) { | ||
if (maxSize > 1) { | ||
var keysLength = allKeys.length; | ||
for (var index = 1; index < keysLength; index++) { | ||
if (isMatchingKey_1(allKeys[index], keyToMatch)) { | ||
if (isMatchingKey(allKeys[index], keyToMatch)) { | ||
return index; | ||
@@ -41,4 +41,3 @@ } | ||
} | ||
var isEqual = options.isEqual; | ||
if (options.maxSize > 1) { | ||
if (maxSize > 1) { | ||
return function getKeyIndex(allKeys, keyToMatch) { | ||
@@ -45,0 +44,0 @@ var keysLength = allKeys.length; |
@@ -20,13 +20,13 @@ var DEFAULT_OPTIONS_KEYS = { | ||
*/ | ||
function createGetKeyIndex(options) { | ||
if (typeof options.isMatchingKey === 'function') { | ||
var isMatchingKey_1 = options.isMatchingKey, maxSize_1 = options.maxSize; | ||
function createGetKeyIndex(_a) { | ||
var isEqual = _a.isEqual, isMatchingKey = _a.isMatchingKey, maxSize = _a.maxSize; | ||
if (typeof isMatchingKey === 'function') { | ||
return function getKeyIndex(allKeys, keyToMatch) { | ||
if (isMatchingKey_1(allKeys[0], keyToMatch)) { | ||
if (isMatchingKey(allKeys[0], keyToMatch)) { | ||
return 0; | ||
} | ||
if (maxSize_1 > 1) { | ||
if (maxSize > 1) { | ||
var keysLength = allKeys.length; | ||
for (var index = 1; index < keysLength; index++) { | ||
if (isMatchingKey_1(allKeys[index], keyToMatch)) { | ||
if (isMatchingKey(allKeys[index], keyToMatch)) { | ||
return index; | ||
@@ -39,4 +39,3 @@ } | ||
} | ||
var isEqual = options.isEqual; | ||
if (options.maxSize > 1) { | ||
if (maxSize > 1) { | ||
return function getKeyIndex(allKeys, keyToMatch) { | ||
@@ -43,0 +42,0 @@ var keysLength = allKeys.length; |
@@ -26,13 +26,13 @@ (function (global, factory) { | ||
*/ | ||
function createGetKeyIndex(options) { | ||
if (typeof options.isMatchingKey === 'function') { | ||
var isMatchingKey_1 = options.isMatchingKey, maxSize_1 = options.maxSize; | ||
function createGetKeyIndex(_a) { | ||
var isEqual = _a.isEqual, isMatchingKey = _a.isMatchingKey, maxSize = _a.maxSize; | ||
if (typeof isMatchingKey === 'function') { | ||
return function getKeyIndex(allKeys, keyToMatch) { | ||
if (isMatchingKey_1(allKeys[0], keyToMatch)) { | ||
if (isMatchingKey(allKeys[0], keyToMatch)) { | ||
return 0; | ||
} | ||
if (maxSize_1 > 1) { | ||
if (maxSize > 1) { | ||
var keysLength = allKeys.length; | ||
for (var index = 1; index < keysLength; index++) { | ||
if (isMatchingKey_1(allKeys[index], keyToMatch)) { | ||
if (isMatchingKey(allKeys[index], keyToMatch)) { | ||
return index; | ||
@@ -45,4 +45,3 @@ } | ||
} | ||
var isEqual = options.isEqual; | ||
if (options.maxSize > 1) { | ||
if (maxSize > 1) { | ||
return function getKeyIndex(allKeys, keyToMatch) { | ||
@@ -49,0 +48,0 @@ var keysLength = allKeys.length; |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self)["micro-memoize"]=n()}(this,function(){"use strict";var P={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0};function j(e){if("function"==typeof e.isMatchingKey){var r=e.isMatchingKey,o=e.maxSize;return function(e,n){if(r(e[0],n))return 0;if(1<o)for(var i=e.length,t=1;t<i;t++)if(r(e[t],n))return t;return-1}}var u=e.isEqual;return 1<e.maxSize?function(e,n){for(var i,t=e.length,r=n.length,o=0;o<t;o++)if((i=e[o]).length===r){for(var a=0;a<r&&u(i[a],n[a]);a++);if(a===r)return o}return-1}:function(e,n){var i=e[0],t=i.length;if(n.length!==t)return-1;for(var r=0;r<t;r++)if(!u(i[r],n[r]))return-1;return 0}}function w(e,n){return e===n||e!=e&&n!=n}function O(e,n,i,t,r){for(var o=t;o--;)e.keys[o+1]=e.keys[o],e.values[o+1]=e.values[o];e.keys[0]=n,e.values[0]=i,r<=t&&(e.keys.length=r,e.values.length=r)}var B=Array.prototype.slice,D=Object.defineProperties;return function(o,e){if(void 0===e&&(e={}),o.isMemoized)return o;var r,a,u,f,c,s,n=e.isEqual,i=void 0===n?w:n,t=e.isMatchingKey,l=e.isPromise,h=void 0!==l&&l,v=e.maxSize,y=void 0===v?1:v,g=e.onCacheAdd,d=e.onCacheChange,p=e.onCacheHit,m=e.transformKey,C=function(e,n){var i={};for(var t in e)i[t]=e[t];for(var t in n)i[t]=n[t];return i}(function(e){var n={};for(var i in e)P[i]||(n[i]=e[i]);return n}(e),{isEqual:i,isMatchingKey:t,isPromise:h,maxSize:y,onCacheAdd:g,onCacheChange:d,onCacheHit:p,transformKey:m}),k=j(C),z=(a=j(r=C),u=r.onCacheChange,f=r.onCacheHit,c="function"==typeof u,s="function"==typeof f,function(i,n){var t=i.keys[0];i.values[0]=i.values[0].then(function(e){return s&&f(i,r,n),c&&u(i,r,n),e}).catch(function(e){var n=a(i.keys,t);throw-1!==n&&(i.keys.splice(n,1),i.values.splice(n,1)),e})}),K=[],x=[],M={keys:K,get size(){return M.keys.length},values:x},b="function"==typeof m,S=!(!m&&!t),q="function"==typeof g,A="function"==typeof d,E="function"==typeof p;function H(){var e=S?B.call(arguments,0):arguments,n=b?m(e):e,i=K.length?k(K,n):-1;if(-1!==i)E&&p(M,C,H),i&&(O(M,K[i],x[i],i,y),A&&d(M,C,H));else{var t=o.apply(this,arguments),r=S?n:B.call(arguments,0);O(M,r,t,K.length,y),h&&z(M,H),q&&g(M,C,H),A&&d(M,C,H)}return x[0]}return D(H,{cache:{configurable:!0,value:M},cacheSnapshot:{configurable:!0,get:function(){return{keys:B.call(M.keys,0),size:M.size,values:B.call(M.values,0)}}},isMemoized:{configurable:!0,value:!0},options:{configurable:!0,value:C}}),H}}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self)["micro-memoize"]=n()}(this,function(){"use strict";var P={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0};function j(e){var u=e.isEqual,r=e.isMatchingKey,o=e.maxSize;return"function"==typeof r?function(e,n){if(r(e[0],n))return 0;if(1<o)for(var t=e.length,i=1;i<t;i++)if(r(e[i],n))return i;return-1}:1<o?function(e,n){for(var t,i=e.length,r=n.length,o=0;o<i;o++)if((t=e[o]).length===r){for(var a=0;a<r&&u(t[a],n[a]);a++);if(a===r)return o}return-1}:function(e,n){var t=e[0],i=t.length;if(n.length!==i)return-1;for(var r=0;r<i;r++)if(!u(t[r],n[r]))return-1;return 0}}function w(e,n){return e===n||e!=e&&n!=n}function O(e,n,t,i,r){for(var o=i;o--;)e.keys[o+1]=e.keys[o],e.values[o+1]=e.values[o];e.keys[0]=n,e.values[0]=t,r<=i&&(e.keys.length=r,e.values.length=r)}var B=Array.prototype.slice,D=Object.defineProperties;return function(o,e){if(void 0===e&&(e={}),o.isMemoized)return o;var r,a,u,f,c,s,n=e.isEqual,t=void 0===n?w:n,i=e.isMatchingKey,l=e.isPromise,h=void 0!==l&&l,v=e.maxSize,y=void 0===v?1:v,g=e.onCacheAdd,d=e.onCacheChange,p=e.onCacheHit,m=e.transformKey,C=function(e,n){var t={};for(var i in e)t[i]=e[i];for(var i in n)t[i]=n[i];return t}(function(e){var n={};for(var t in e)P[t]||(n[t]=e[t]);return n}(e),{isEqual:t,isMatchingKey:i,isPromise:h,maxSize:y,onCacheAdd:g,onCacheChange:d,onCacheHit:p,transformKey:m}),k=j(C),z=(a=j(r=C),u=r.onCacheChange,f=r.onCacheHit,c="function"==typeof u,s="function"==typeof f,function(t,n){var i=t.keys[0];t.values[0]=t.values[0].then(function(e){return s&&f(t,r,n),c&&u(t,r,n),e}).catch(function(e){var n=a(t.keys,i);throw-1!==n&&(t.keys.splice(n,1),t.values.splice(n,1)),e})}),K=[],b=[],x={keys:K,get size(){return x.keys.length},values:b},M="function"==typeof m,S=!(!m&&!i),q="function"==typeof g,A="function"==typeof d,E="function"==typeof p;function H(){var e=S?B.call(arguments,0):arguments,n=M?m(e):e,t=K.length?k(K,n):-1;if(-1!==t)E&&p(x,C,H),t&&(O(x,K[t],b[t],t,y),A&&d(x,C,H));else{var i=o.apply(this,arguments),r=S?n:B.call(arguments,0);O(x,r,i,K.length,y),h&&z(x,H),q&&g(x,C,H),A&&d(x,C,H)}return b[0]}return D(H,{cache:{configurable:!0,value:x},cacheSnapshot:{configurable:!0,get:function(){return{keys:B.call(x.keys,0),size:x.size,values:B.call(x.values,0)}}},isMemoized:{configurable:!0,value:!0},options:{configurable:!0,value:C}}),H}}); |
@@ -7,3 +7,5 @@ const fs = require('fs'); | ||
const SOURCE = path.join(__dirname, pkg.module); | ||
const DESTINATION = path.join(__dirname, pkg.module.replace('esm.js', 'mjs')); | ||
const SOURCE_MAP = `${SOURCE}.map`; | ||
const DESTINATION = path.join(__dirname, 'mjs', 'index.mjs'); | ||
const DESTINATION_MAP = `${DESTINATION}.map`; | ||
@@ -16,8 +18,28 @@ const getFileName = filename => { | ||
fs.copyFile(SOURCE, DESTINATION, error => { | ||
if (error) { | ||
throw error; | ||
try { | ||
if (!fs.existsSync(path.join(__dirname, 'mjs'))) { | ||
fs.mkdirSync(path.join(__dirname, 'mjs')); | ||
} | ||
fs.copyFileSync(SOURCE, DESTINATION); | ||
const contents = fs | ||
.readFileSync(DESTINATION, { encoding: 'utf8' }) | ||
.replace(/\/\/# sourceMappingURL=(.*)/, (match, value) => { | ||
return match.replace(value, 'index.mjs.map'); | ||
}); | ||
fs.writeFileSync(DESTINATION, contents, { encoding: 'utf8' }); | ||
console.log(`Copied ${getFileName(SOURCE)} to ${getFileName(DESTINATION)}`); | ||
}); | ||
fs.copyFileSync(SOURCE_MAP, DESTINATION_MAP); | ||
console.log( | ||
`Copied ${getFileName(SOURCE_MAP)} to ${getFileName(DESTINATION_MAP)}`, | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
process.exit(1); | ||
} |
@@ -85,3 +85,3 @@ { | ||
"build:mjs": "node ./es-to-mjs.js", | ||
"clean": "rimraf dist", | ||
"clean": "rimraf dist && rimraf mjs", | ||
"dev": "NODE_ENV=development webpack-dev-server --colors --progress --config=webpack/webpack.config.js", | ||
@@ -100,3 +100,3 @@ "dist": "npm run clean && npm run build && npm run build:mjs", | ||
"types": "./index.d.ts", | ||
"version": "3.0.0-beta.6" | ||
"version": "3.0.0" | ||
} |
107
README.md
@@ -54,3 +54,3 @@ # micro-memoize | ||
```javascript | ||
const memoize = require('micro-memoize').default; | ||
const memoize = require('micro-memoize'); | ||
``` | ||
@@ -61,11 +61,2 @@ | ||
```javascript | ||
// ES2015+ | ||
import memoize from 'micro-memoize'; | ||
// CommonJS | ||
const memoize = require('micro-memoize').default; | ||
// old-school | ||
const memoize = window.memoize; | ||
const assembleToObject = (one, two) => { | ||
@@ -188,3 +179,3 @@ return { one, two }; | ||
`boolean`, _defaults to `boolean`_ | ||
`boolean`, _defaults to `false`_ | ||
@@ -248,3 +239,3 @@ Identifies the value returned from the method as a `Promise`, which will result in one of two possible scenarios: | ||
`function(cache: Cache, options: Options): void`, _defaults to noop_ | ||
`function(cache: Cache, options: Options): void` | ||
@@ -282,3 +273,3 @@ Callback method that executes whenever the cache is added to. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
`function(cache: Cache, options: Options): void`, _defaults to noop_ | ||
`function(cache: Cache, options: Options): void` | ||
@@ -316,3 +307,3 @@ Callback method that executes whenever the cache is added to or the order is updated. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
`function(cache: Cache, options: Options): void`, _defaults to noop_ | ||
`function(cache: Cache, options: Options): void` | ||
@@ -451,2 +442,4 @@ Callback method that executes whenever the cache is hit, whether the order is updated or not. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
**NOTE**: Not all libraries tested support multiple parameters out of the box, but support the ability to pass a custom `resolver`. Because these often need to resolve to a string value, [a common suggestion](https://github.com/lodash/lodash/issues/2115) is to just `JSON.stringify` the arguments, so that is what is used when needed. | ||
#### Single parameter (primitive only) | ||
@@ -456,13 +449,14 @@ | ||
| | Operations / second | Relative margin of error | | ||
| ----------------- | ------------------- | ------------------------ | | ||
| fast-memoize | 219,525,943 | 0.56% | | ||
| **micro-memoize** | **76,004,234** | **1.12%** | | ||
| lodash | 26,920,988 | 0.65% | | ||
| underscore | 24,126,335 | 0.73% | | ||
| memoizee | 16,575,237 | 0.74% | | ||
| lru-memoize | 8,016,237 | 1.58% | | ||
| Addy Osmani | 6,476,533 | 0.96% | | ||
| memoizerific | 5,511,233 | 0.78% | | ||
| ramda | 1,107,319 | 0.68% | | ||
| | Operations / second | | ||
| ----------------- | ------------------- | | ||
| fast-memoize | 47,878,927 | | ||
| **micro-memoize** | **40,373,458** | | ||
| lru-memoize | 39,989,466 | | ||
| Addy Osmani | 27,782,914 | | ||
| lodash | 26,352,951 | | ||
| ramda | 22,475,634 | | ||
| underscore | 22,030,431 | | ||
| mem | 20,299,797 | | ||
| memoizee | 16,985,364 | | ||
| memoizerific | 5,823,373 | | ||
@@ -473,13 +467,14 @@ #### Single parameter (complex object) | ||
| | Operations / second | Relative margin of error | | ||
| ----------------- | ------------------- | ------------------------ | | ||
| **micro-memoize** | **60,533,096** | **0.68%** | | ||
| memoizee | 11,601,186 | 0.82% | | ||
| lodash | 8,017,634 | 0.77% | | ||
| underscore | 7,910,175 | 0.76% | | ||
| lru-memoize | 6,878,249 | 1.12% | | ||
| memoizerific | 4.377,062 | 0.74% | | ||
| Addy Osmani | 1,829,256 | 0.74% | | ||
| fast-memoize | 1,468,272 | 0.67% | | ||
| ramda | 213,118 | 0.84% | | ||
| | Operations / second | | ||
| ----------------- | ------------------- | | ||
| **micro-memoize** | **30,040,598** | | ||
| lodash | 29,298,955 | | ||
| lru-memoize | 21,341,877 | | ||
| memoizee | 11,149,801 | | ||
| memoizerific | 5,542,238 | | ||
| ramda | 2,112,667 | | ||
| underscore | 2,089,144 | | ||
| Addy Osmani | 1,973,732 | | ||
| mem | 1,853,416 | | ||
| fast-memoize | 1,527,852 | | ||
@@ -490,10 +485,14 @@ #### Multiple parameters (primitives only) | ||
| | Operations / second | Relative margin of error | | ||
| ----------------- | ------------------- | ------------------------ | | ||
| **micro-memoize** | **49,690,821** | **1.26%** | | ||
| memoizee | 10,425,265 | 0.76% | | ||
| lru-memoize | 6,165,918 | 0.76% | | ||
| memoizerific | 4,587,050 | 0.72% | | ||
| Addy Osmani | 3,409,941 | 0.67% | | ||
| fast-memoize | 1,214,616 | 0.66% | | ||
| | Operations / second | | ||
| ----------------- | ------------------- | | ||
| **micro-memoize** | **27,606,297** | | ||
| lru-memoize | 17,917,076 | | ||
| memoizee | 6,778,193 | | ||
| Addy Osmani | 5,787,376 | | ||
| memoizerific | 4,651,947 | | ||
| mem | 2,402,371 | | ||
| ramda | 1,548,197 | | ||
| underscore | 1,522,849 | | ||
| lodash | 1,349,424 | | ||
| fast-memoize | 1,300,765 | | ||
@@ -504,10 +503,14 @@ #### Multiple parameters (complex objects) | ||
| | Operations / second | Relative margin of error | | ||
| ----------------- | ------------------- | ------------------------ | | ||
| **micro-memoize** | **47,300,339** | **1.20%** | | ||
| memoizee | 7,487,582 | 0.73% | | ||
| lru-memoize | 6,287,893 | 1.15% | | ||
| memoizerific | 3,537,690 | 0.75% | | ||
| Addy Osmani | 936,273 | 0.70% | | ||
| fast-memoize | 808,141 | 0.68% | | ||
| | Operations / second | | ||
| ----------------- | ------------------- | | ||
| **micro-memoize** | **25,845,573** | | ||
| lru-memoize | 17,060,727 | | ||
| memoizee | 6,708,619 | | ||
| memoizerific | 4,719,046 | | ||
| mem | 1,045,712 | | ||
| ramda | 962,299 | | ||
| underscore | 952,825 | | ||
| Addy Osmani | 941,157 | | ||
| fast-memoize | 872,483 | | ||
| lodash | 871,964 | | ||
@@ -514,0 +517,0 @@ ## Browser support |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
441326
26
2649
0
537