Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

micro-memoize

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro-memoize - npm Package Compare versions

Comparing version 3.0.0-beta.3 to 3.0.0-beta.4

37

__tests__/utils.ts
import {
createAreKeysEqual,
// createAreKeysEqual,
createGetKeyIndex,

@@ -10,37 +10,2 @@ createUpdateAsyncCache,

describe('areKeysEqual', () => {
it('will return false when the length of the keys are different', () => {
const isEqual = (o1: any, o2: any) => o1 === o2;
const areKeysEqual = createAreKeysEqual(isEqual);
const keys1: any[] = [];
const keys2 = ['key'];
expect(areKeysEqual(keys1, keys2)).toEqual(false);
});
it('will return false when the keys have different values', () => {
const isEqual = (o1: any, o2: any) => o1 === o2;
const areKeysEqual = createAreKeysEqual(isEqual);
const keys1 = ['key'];
const keys2 = ['other key'];
expect(areKeysEqual(keys1, keys2)).toEqual(false);
});
it('will return true when the keys have equal values', () => {
const isEqual = (o1: any, o2: any) => o1 === o2;
const areKeysEqual = createAreKeysEqual(isEqual);
const keys1 = ['key'];
const keys2 = ['key'];
expect(areKeysEqual(keys1, keys2)).toEqual(true);
});
});
describe('getKeyIndex', () => {

@@ -47,0 +12,0 @@ it('will return the index of the match found', () => {

12

benchmarks/index.js

@@ -35,5 +35,3 @@ const _ = require('lodash');

const memoizerific = require('memoizerific');
const memoize =
require('../dist/micro-memoize.cjs').default ||
require('../dist/micro-memoize.cjs');
const memoize = require('../dist/micro-memoize.cjs');
const ramda = require('ramda').memoizeWith(resolveArguments);

@@ -202,2 +200,3 @@ const underscore = require('underscore').memoize;

const suite = createSuite({
minTime: 1000,
onComplete(results) {

@@ -269,3 +268,2 @@ const combinedResults = Object.keys(results)

},
minTime: 3000,
onResult({ name, stats }) {

@@ -282,3 +280,7 @@ console.log(

suite.add(name, 'singular primitive', () => {
fn(number);
try {
fn(number);
} catch (error) {
console.error(error);
}
});

@@ -285,0 +287,0 @@ });

@@ -28,64 +28,60 @@ 'use strict';

function createAreKeysEqual(isEqual) {
/**
* @function areKeysEqual
*
* @description
* are the keys shallowly equal to one another
*
* @param key1 the keys array to test against
* @param key2 the keys array to test
* @returns are the keys shallowly equal
*/
return function areKeysEqual(key1, key2) {
var length = key1.length;
if (key2.length !== length) {
return false;
function createGetKeyIndex(_a) {
var isEqual = _a.isEqual, isMatchingKey = _a.isMatchingKey, maxSize = _a.maxSize;
return function getKeyIndex(allKeys, keyToMatch) {
if (isMatchingKey) {
if (isMatchingKey(allKeys[0], keyToMatch)) {
return 0;
}
if (maxSize > 1) {
var keysLength = allKeys.length;
var index_1 = 1;
while (index_1 < keysLength) {
if (isMatchingKey(allKeys[index_1], keyToMatch)) {
return index_1;
}
index_1++;
}
}
return -1;
}
if (length === 1) {
return isEqual(key1[0], key2[0]);
if (maxSize > 1) {
var keysLength = allKeys.length;
var keyLength_1 = keyToMatch.length;
var index_2 = 0;
var existingKey_1;
var argIndex = void 0;
while (index_2 < keysLength) {
existingKey_1 = allKeys[index_2];
if (existingKey_1.length === keyLength_1) {
argIndex = 0;
while (argIndex < keyLength_1) {
if (!isEqual(existingKey_1[argIndex], keyToMatch[argIndex])) {
break;
}
argIndex++;
}
if (argIndex === keyLength_1) {
return index_2;
}
}
index_2++;
}
return -1;
}
var existingKey = allKeys[0];
var keyLength = existingKey.length;
if (keyToMatch.length !== keyLength) {
return -1;
}
var index = 0;
while (index < length) {
if (!isEqual(key1[index], key2[index])) {
return false;
while (index < keyLength) {
if (!isEqual(existingKey[index], keyToMatch[index])) {
return -1;
}
index++;
}
return true;
return 0;
};
}
function createGetKeyIndex(options) {
var maxSize = options.maxSize;
var areKeysEqual = typeof options.isMatchingKey === 'function'
? options.isMatchingKey
: createAreKeysEqual(options.isEqual);
/**
* @function getKeyIndex
*
* @description
* get the index of the matching key
*
* @param allKeys the list of all available keys
* @param keyToMatch the key to try to match
*
* @returns {number} the index of the matching key value, or -1
*/
return function getKeyIndex(allKeys, keyToMatch) {
if (areKeysEqual(allKeys[0], keyToMatch)) {
return 0;
}
if (maxSize > 1) {
var length = maxSize > allKeys.length ? allKeys.length : maxSize;
var index = 1;
while (index < length) {
if (areKeysEqual(allKeys[index], keyToMatch)) {
return index;
}
index++;
}
}
return -1;
};
}
/**

@@ -174,3 +170,3 @@ * @function isSameValueZero

var keyIndex = getKeyIndex(cache.keys, key);
if (~keyIndex) {
if (keyIndex !== -1) {
cache.keys.splice(keyIndex, 1);

@@ -222,7 +218,5 @@ cache.values.splice(keyIndex, 1);

: arguments;
var key = canTransformKey
? transformKey(normalizedArgs)
: normalizedArgs;
var key = canTransformKey ? transformKey(normalizedArgs) : normalizedArgs;
var keyIndex = keys.length ? getKeyIndex(keys, key) : -1;
if (~keyIndex) {
if (keyIndex !== -1) {
shouldUpdateOnHit && onCacheHit(cache, normalizedOptions, memoized);

@@ -237,5 +231,3 @@ if (keyIndex) {

var newValue = fn.apply(this, arguments);
var newKey = shouldCloneArguments
? key
: slice.call(normalizedArgs, 0);
var newKey = shouldCloneArguments ? key : slice.call(arguments, 0);
orderByLru(cache, newKey, newValue, keys.length, maxSize);

@@ -242,0 +234,0 @@ isPromise && updateAsyncCache(cache, memoized);

@@ -26,64 +26,60 @@ /*! *****************************************************************************

function createAreKeysEqual(isEqual) {
/**
* @function areKeysEqual
*
* @description
* are the keys shallowly equal to one another
*
* @param key1 the keys array to test against
* @param key2 the keys array to test
* @returns are the keys shallowly equal
*/
return function areKeysEqual(key1, key2) {
var length = key1.length;
if (key2.length !== length) {
return false;
function createGetKeyIndex(_a) {
var isEqual = _a.isEqual, isMatchingKey = _a.isMatchingKey, maxSize = _a.maxSize;
return function getKeyIndex(allKeys, keyToMatch) {
if (isMatchingKey) {
if (isMatchingKey(allKeys[0], keyToMatch)) {
return 0;
}
if (maxSize > 1) {
var keysLength = allKeys.length;
var index_1 = 1;
while (index_1 < keysLength) {
if (isMatchingKey(allKeys[index_1], keyToMatch)) {
return index_1;
}
index_1++;
}
}
return -1;
}
if (length === 1) {
return isEqual(key1[0], key2[0]);
if (maxSize > 1) {
var keysLength = allKeys.length;
var keyLength_1 = keyToMatch.length;
var index_2 = 0;
var existingKey_1;
var argIndex = void 0;
while (index_2 < keysLength) {
existingKey_1 = allKeys[index_2];
if (existingKey_1.length === keyLength_1) {
argIndex = 0;
while (argIndex < keyLength_1) {
if (!isEqual(existingKey_1[argIndex], keyToMatch[argIndex])) {
break;
}
argIndex++;
}
if (argIndex === keyLength_1) {
return index_2;
}
}
index_2++;
}
return -1;
}
var existingKey = allKeys[0];
var keyLength = existingKey.length;
if (keyToMatch.length !== keyLength) {
return -1;
}
var index = 0;
while (index < length) {
if (!isEqual(key1[index], key2[index])) {
return false;
while (index < keyLength) {
if (!isEqual(existingKey[index], keyToMatch[index])) {
return -1;
}
index++;
}
return true;
return 0;
};
}
function createGetKeyIndex(options) {
var maxSize = options.maxSize;
var areKeysEqual = typeof options.isMatchingKey === 'function'
? options.isMatchingKey
: createAreKeysEqual(options.isEqual);
/**
* @function getKeyIndex
*
* @description
* get the index of the matching key
*
* @param allKeys the list of all available keys
* @param keyToMatch the key to try to match
*
* @returns {number} the index of the matching key value, or -1
*/
return function getKeyIndex(allKeys, keyToMatch) {
if (areKeysEqual(allKeys[0], keyToMatch)) {
return 0;
}
if (maxSize > 1) {
var length = maxSize > allKeys.length ? allKeys.length : maxSize;
var index = 1;
while (index < length) {
if (areKeysEqual(allKeys[index], keyToMatch)) {
return index;
}
index++;
}
}
return -1;
};
}
/**

@@ -172,3 +168,3 @@ * @function isSameValueZero

var keyIndex = getKeyIndex(cache.keys, key);
if (~keyIndex) {
if (keyIndex !== -1) {
cache.keys.splice(keyIndex, 1);

@@ -220,7 +216,5 @@ cache.values.splice(keyIndex, 1);

: arguments;
var key = canTransformKey
? transformKey(normalizedArgs)
: normalizedArgs;
var key = canTransformKey ? transformKey(normalizedArgs) : normalizedArgs;
var keyIndex = keys.length ? getKeyIndex(keys, key) : -1;
if (~keyIndex) {
if (keyIndex !== -1) {
shouldUpdateOnHit && onCacheHit(cache, normalizedOptions, memoized);

@@ -235,5 +229,3 @@ if (keyIndex) {

var newValue = fn.apply(this, arguments);
var newKey = shouldCloneArguments
? key
: slice.call(normalizedArgs, 0);
var newKey = shouldCloneArguments ? key : slice.call(arguments, 0);
orderByLru(cache, newKey, newValue, keys.length, maxSize);

@@ -240,0 +232,0 @@ isPromise && updateAsyncCache(cache, memoized);

@@ -32,64 +32,60 @@ (function (global, factory) {

function createAreKeysEqual(isEqual) {
/**
* @function areKeysEqual
*
* @description
* are the keys shallowly equal to one another
*
* @param key1 the keys array to test against
* @param key2 the keys array to test
* @returns are the keys shallowly equal
*/
return function areKeysEqual(key1, key2) {
var length = key1.length;
if (key2.length !== length) {
return false;
function createGetKeyIndex(_a) {
var isEqual = _a.isEqual, isMatchingKey = _a.isMatchingKey, maxSize = _a.maxSize;
return function getKeyIndex(allKeys, keyToMatch) {
if (isMatchingKey) {
if (isMatchingKey(allKeys[0], keyToMatch)) {
return 0;
}
if (maxSize > 1) {
var keysLength = allKeys.length;
var index_1 = 1;
while (index_1 < keysLength) {
if (isMatchingKey(allKeys[index_1], keyToMatch)) {
return index_1;
}
index_1++;
}
}
return -1;
}
if (length === 1) {
return isEqual(key1[0], key2[0]);
if (maxSize > 1) {
var keysLength = allKeys.length;
var keyLength_1 = keyToMatch.length;
var index_2 = 0;
var existingKey_1;
var argIndex = void 0;
while (index_2 < keysLength) {
existingKey_1 = allKeys[index_2];
if (existingKey_1.length === keyLength_1) {
argIndex = 0;
while (argIndex < keyLength_1) {
if (!isEqual(existingKey_1[argIndex], keyToMatch[argIndex])) {
break;
}
argIndex++;
}
if (argIndex === keyLength_1) {
return index_2;
}
}
index_2++;
}
return -1;
}
var existingKey = allKeys[0];
var keyLength = existingKey.length;
if (keyToMatch.length !== keyLength) {
return -1;
}
var index = 0;
while (index < length) {
if (!isEqual(key1[index], key2[index])) {
return false;
while (index < keyLength) {
if (!isEqual(existingKey[index], keyToMatch[index])) {
return -1;
}
index++;
}
return true;
return 0;
};
}
function createGetKeyIndex(options) {
var maxSize = options.maxSize;
var areKeysEqual = typeof options.isMatchingKey === 'function'
? options.isMatchingKey
: createAreKeysEqual(options.isEqual);
/**
* @function getKeyIndex
*
* @description
* get the index of the matching key
*
* @param allKeys the list of all available keys
* @param keyToMatch the key to try to match
*
* @returns {number} the index of the matching key value, or -1
*/
return function getKeyIndex(allKeys, keyToMatch) {
if (areKeysEqual(allKeys[0], keyToMatch)) {
return 0;
}
if (maxSize > 1) {
var length = maxSize > allKeys.length ? allKeys.length : maxSize;
var index = 1;
while (index < length) {
if (areKeysEqual(allKeys[index], keyToMatch)) {
return index;
}
index++;
}
}
return -1;
};
}
/**

@@ -178,3 +174,3 @@ * @function isSameValueZero

var keyIndex = getKeyIndex(cache.keys, key);
if (~keyIndex) {
if (keyIndex !== -1) {
cache.keys.splice(keyIndex, 1);

@@ -226,7 +222,5 @@ cache.values.splice(keyIndex, 1);

: arguments;
var key = canTransformKey
? transformKey(normalizedArgs)
: normalizedArgs;
var key = canTransformKey ? transformKey(normalizedArgs) : normalizedArgs;
var keyIndex = keys.length ? getKeyIndex(keys, key) : -1;
if (~keyIndex) {
if (keyIndex !== -1) {
shouldUpdateOnHit && onCacheHit(cache, normalizedOptions, memoized);

@@ -241,5 +235,3 @@ if (keyIndex) {

var newValue = fn.apply(this, arguments);
var newKey = shouldCloneArguments
? key
: slice.call(normalizedArgs, 0);
var newKey = shouldCloneArguments ? key : slice.call(arguments, 0);
orderByLru(cache, newKey, newValue, keys.length, maxSize);

@@ -246,0 +238,0 @@ isPromise && updateAsyncCache(cache, memoized);

@@ -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";function w(e){var o,r=e.maxSize,a="function"==typeof e.isMatchingKey?e.isMatchingKey:(o=e.isEqual,function(e,n){var t=e.length;if(n.length!==t)return!1;if(1===t)return o(e[0],n[0]);for(var i=0;i<t;){if(!o(e[i],n[i]))return!1;i++}return!0});return function(e,n){if(a(e[0],n))return 0;if(1<r)for(var t=r>e.length?e.length:r,i=1;i<t;){if(a(e[i],n))return i;i++}return-1}}function A(e,n){return e===n||e!=e&&n!=n}function E(e,n,t,i,o){for(var r=i;r--;)e.keys[r+1]=e.keys[r],e.values[r+1]=e.values[r];e.keys[0]=n,e.values[0]=t,o<=i&&(e.keys.length=o,e.values.length=o)}var H=Array.prototype.slice;return function(r,e){if(void 0===e&&(e={}),r.isMemoized)return r;var o,a,u,f,c,s,n=e.isEqual,t=void 0===n?A:n,i=e.isMatchingKey,l=e.isPromise,h=void 0!==l&&l,y=e.maxSize,v=void 0===y?1:y,g=e.onCacheAdd,p=e.onCacheChange,d=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,n){var t={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&n.indexOf(i)<0&&(t[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++)n.indexOf(i[o])<0&&(t[i[o]]=e[i[o]])}return t}(e,["isEqual","isMatchingKey","isPromise","maxSize","onCacheAdd","onCacheChange","onCacheHit","transformKey"]),{isEqual:t,isMatchingKey:i,isPromise:h,maxSize:v,onCacheAdd:g,onCacheChange:p,onCacheHit:d,transformKey:m}),b=w(C),k=(a=w(o=C),u=o.onCacheChange,f=o.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,o,n),c&&u(t,o,n),e}).catch(function(e){var n=a(t.keys,i);throw~n&&(t.keys.splice(n,1),t.values.splice(n,1)),e})}),z=[],O=[],x={keys:z,get size(){return x.keys.length},values:O},K="function"==typeof m,M=!(!m&&!i),P="function"==typeof g,S="function"==typeof p,j="function"==typeof d;function q(){var e=M?H.call(arguments,0):arguments,n=K?m(e):e,t=z.length?b(z,n):-1;if(~t)j&&d(x,C,q),t&&(E(x,z[t],O[t],t,v),S&&p(x,C,q));else{var i=r.apply(this,arguments),o=M?n:H.call(e,0);E(x,o,i,z.length,v),h&&k(x,q),P&&g(x,C,q),S&&p(x,C,q)}return O[0]}return Object.defineProperties(q,{cache:{configurable:!0,value:x},cacheSnapshot:{configurable:!0,get:function(){return{keys:H.call(x.keys,0),size:x.size,values:H.call(x.values,0)}}},isMemoized:{configurable:!0,value:!0},options:{configurable:!0,value:C}}),q}});
!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";function w(e){var l=e.isEqual,h=e.isMatchingKey,y=e.maxSize;return function(e,n){if(h){if(h(e[0],n))return 0;if(1<y)for(var t=e.length,i=1;i<t;){if(h(e[i],n))return i;i++}return-1}if(1<y){t=e.length;for(var r,o=n.length,a=0,f=void 0;a<t;){if((r=e[a]).length===o){for(f=0;f<o&&l(r[f],n[f]);)f++;if(f===o)return a}a++}return-1}var u=e[0],c=u.length;if(n.length!==c)return-1;for(var s=0;s<c;){if(!l(u[s],n[s]))return-1;s++}return 0}}function A(e,n){return e===n||e!=e&&n!=n}function E(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 H=Array.prototype.slice;return function(o,e){if(void 0===e&&(e={}),o.isMemoized)return o;var r,a,f,u,c,s,n=e.isEqual,t=void 0===n?A:n,i=e.isMatchingKey,l=e.isPromise,h=void 0!==l&&l,y=e.maxSize,v=void 0===y?1:y,g=e.onCacheAdd,p=e.onCacheChange,d=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,n){var t={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&n.indexOf(i)<0&&(t[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(e);r<i.length;r++)n.indexOf(i[r])<0&&(t[i[r]]=e[i[r]])}return t}(e,["isEqual","isMatchingKey","isPromise","maxSize","onCacheAdd","onCacheChange","onCacheHit","transformKey"]),{isEqual:t,isMatchingKey:i,isPromise:h,maxSize:v,onCacheAdd:g,onCacheChange:p,onCacheHit:d,transformKey:m}),b=w(C),k=(a=w(r=C),f=r.onCacheChange,u=r.onCacheHit,c="function"==typeof f,s="function"==typeof u,function(t,n){var i=t.keys[0];t.values[0]=t.values[0].then(function(e){return s&&u(t,r,n),c&&f(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})}),z=[],O=[],x={keys:z,get size(){return x.keys.length},values:O},K="function"==typeof m,P=!(!m&&!i),S="function"==typeof g,M="function"==typeof p,j="function"==typeof d;function q(){var e=P?H.call(arguments,0):arguments,n=K?m(e):e,t=z.length?b(z,n):-1;if(-1!==t)j&&d(x,C,q),t&&(E(x,z[t],O[t],t,v),M&&p(x,C,q));else{var i=o.apply(this,arguments),r=P?n:H.call(arguments,0);E(x,r,i,z.length,v),h&&k(x,q),S&&g(x,C,q),M&&p(x,C,q)}return O[0]}return Object.defineProperties(q,{cache:{configurable:!0,value:x},cacheSnapshot:{configurable:!0,get:function(){return{keys:H.call(x.keys,0),size:x.size,values:H.call(x.values,0)}}},isMemoized:{configurable:!0,value:!0},options:{configurable:!0,value:C}}),q}});

@@ -35,3 +35,3 @@ {

"mini-bench": "^1.0.0",
"ora": "^3.0.0",
"ora": "^3.1.0",
"performance-now": "^2.1.0",

@@ -100,3 +100,3 @@ "ramda": "^0.26.1",

"types": "./index.d.ts",
"version": "3.0.0-beta.3"
"version": "3.0.0-beta.4"
}

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc