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

@swarmy/lru-cache

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@swarmy/lru-cache - npm Package Compare versions

Comparing version 2.0.3 to 2.0.4

2

build/lru-cache.js

@@ -1,1 +0,1 @@

module.exports=function(n){var r={};function o(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,o),t.l=!0,t.exports}return o.m=n,o.c=r,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)o.d(n,r,function(e){return t[e]}.bind(null,r));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=0)}([function(e,t,n){"use strict";n.r(t);var l=function(e,t){if(e!==t.newest){if(null===t.newest)return t.newest=e,void(t.oldest=e);var n=e.next,r=e.prev,o=t.newest;e.next=null,e.prev=t.newest,t.newest=e,null!==n&&(n.prev=r,t.oldest===e&&(t.oldest=n)),null!==o&&((o.next=e).prev=o),null!==r&&(r.next=n)}},r=function(e,t,n){null===e.next?t.newest=e.prev:e.next.prev=e.prev,null===e.prev?t.oldest=e.next:e.prev.next=e.next,n.delete(e.key)},i=function(e,t,n){var r,o,u,a=[];if(null===e)return a;for(;n.size>e;)a.push((o=n,u=void 0,u=(r=t).oldest,o.delete(r.oldest.key)&&(r.oldest=r.oldest.next,null!==r.oldest&&(r.oldest.prev=null)),delete u.next,delete u.prev,u));return a};function c(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,t=this instanceof c?this:Object.create(c.prototype),o=e;0===o&&(o=null);var u=new Map,a={newest:null,oldest:null};return t.setMaxSize=function(e){return(o=e)<1&&(o=null),i(o,a,u)},t.getMaxSize=function(){return o},t.getSize=function(){return u.size},t.set=function(e,t){var n=u.get(e);void 0===n?(n={key:e,value:t,next:null,prev:null},u.set(e,n)):n.value=t,l(n,a);var r=i(o,a,u);return 1===r.length?r[0]:null},t.get=function(e){var t=u.get(e);return void 0===t?t:(l(t,a),t.value)},t.getWithoutLruChange=function(e){try{return u.get(e).value}catch(e){}},t.delete=function(e){var t=u.get(e);return void 0!==t&&(r(t,a,u),!0)},t.forEach=function(e){for(var t=a.oldest;null!==t;)e(t.value,t.key),t=t.next},t.map=function(e){for(var t=a.oldest,n=[];null!==t;)n.push(e(t.value,t.key)),t=t.next;return n},t.clear=function(){var e=t.map(function(e,t){return{key:t,value:e}});return u.clear(),a.newest=null,a.oldest=null,e},t}function d(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function a(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(e){f(t,e,n[e])})}return t}function f(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n.d(t,"registerCacheChangedHandler",function(){return p}),n.d(t,"cacheTransaction",function(){return S}),n.d(t,"getCache",function(){return K}),n.d(t,"clearAllCaches",function(){return L});var s=500,o=0,v=new Map,y=new Map,h=new Set,u=function(n,e){null===e||Array.isArray(e)&&0===e.length?h.add(n):(Array.isArray(e)?e:[e]).forEach(function(e){var t=y.get(e);void 0===t&&(t=new Set,y.set(e,t)),t.add(n)})},g=function(t,e){null===e||Array.isArray(e)&&0===e.length?h.delete(t):(Array.isArray(e)?e:[e]).forEach(function(e){y.get(e).delete(t)})},p=function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,n=o;o+=1;var r={changedHandler:e,valueTypes:t,isActive:!0,unregister:function(){v.delete(n),g(n,t)},activate:function(){r.isActive=!0,u(n,t)},deactivate:function(){r.isActive=!1,g(n,t)},isRegistered:function(){return v.has(n)}};return v.set(n,r),u(n,t),r},w=function(n){var e,t,r,o=(e=n.valueTypes,t="string"==typeof e?[e]:e,r=new Set,t.forEach(function(e){var t=y.get(e);void 0!==t&&t.forEach(function(e){r.add(e)})}),h.forEach(function(e){r.add(e)}),r),u=[],a=0;if(o.forEach(function(e){var t=v.get(e);try{t.changedHandler(n)}catch(e){u.push(e)}finally{a+=1}}),0<u.length){var l="handleTransactionChangeObject: "+String(u.length)+" of "+String(a)+" handlers threw an error: ";u.forEach(function(e){l+=e.message+", "});var i=new Error(l);throw i.errors=u,i}},m=null,b=0,A=0,S=function(e){if(null===m&&(m={valueTypes:new Set}),A+=1,"function"==typeof e.finally)e.finally(function(){if(0===(A-=1))try{w(m)}finally{m=null,b=0}});else try{e()}finally{if(0===(A-=1))try{w(m)}finally{m=null,b=0}}},x=function(t,e,n,r){var o=m,u=null!==o;null===o&&(o={valueTypes:new Set}),o.valueTypes.has(t)?o[t][n].push(a({},e,{order:b++})):(o.valueTypes.add(t),o[t]=f({},n,[a({},e,{order:b++})]),r.forEach(function(e){o[t][e]=[]})),u||w(o)},E=!0,O=function(e,t){E&&x(e,t,"clearRemoves",["inserts","lruRemoves","deleteRemoves"])},j=function(e,t){E&&x(e,t,"lruRemoves",["clearRemoves","inserts","deleteRemoves"])},M=function(e,t){E&&x(e,t,"deleteRemoves",["clearRemoves","lruRemoves","inserts"])},R=function(o){return function(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];return new Promise(function(t,n){setTimeout(function(){try{var e=o.apply(void 0,r);t(e)}catch(e){n(e)}},0)})}},k=function(e,t){if(function(e){if(0<h.size)return!0;var t=y.get(e);return void 0!==t&&0<t.size}(e))S(t);else try{E=!1,t()}finally{E=!0}},z=function(c,f,s,e,v){if(!Array.isArray(e))throw new Error("LruCache::setAll: keyValueAlternateKeysArray must be an array");k(c,function(){e.forEach(function(e){var t=e.key,n=e.value,r=e.alternateKeys,o=f.get(t),u=Array.isArray(r)?r:[];0===u.length&&"string"==typeof r&&(u=[r]),u.forEach(function(e){if(s.has(e)&&s.get(e)!==t)throw new Error("LruCache::setAll: alternate key '"+e+"' is given for key '"+t+"' and value type '"+c+"' but is already used for key '"+s.get(e)+"'")}),u=new Set(u),void 0===o?o={key:t,value:n,alternateKeys:u}:(o.value=n,o.alternateKeys=new Set(d(o.alternateKeys).concat(d(u))));var a,l,i=f.set(t,o);u.forEach(function(e){s.set(e,t)}),a=c,l=o,E&&x(a,l,"inserts",["clearRemoves","lruRemoves","deleteRemoves"]),null!==i&&(i.value.alternateKeys.forEach(function(e){s.delete(e)}),v&&j(c,i.value))})})},T=function(e,t,n){var r=n(e);return void 0===r&&t.has(e)&&(r=n(t.get(e))),r};function C(n){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:s,t=this instanceof C?this:Object.create(C.prototype),r=c(e),o=new Map,u=!1,a=!1;return t.dispatchLruRemoves=function(e){u=e},t.dispatchClearRemoves=function(e){a=e},t.setAll=function(e){z(n,r,o,e,u)},t.setAllAsync=R(t.setAll),t.set=function(e){t.setAll([e])},t.setAsync=R(t.set),t.get=function(e){var t=T(e,o,r.get);return void 0===t?t:t.value},t.getWithoutLruChange=function(e){var t=T(e,o,r.getWithoutLruChange);return void 0===t?t:t.value},t.delete=function(e){var t=T(e,o,r.getWithoutLruChange);return void 0!==t&&(r.delete(t.key),t.alternateKeys.forEach(function(e){o.delete(e)}),k(n,function(){M(n,t)}),!0)},t.forEach=r.forEach,t.getEntries=function(){return r.map(function(e){return e})},t.clear=function(){var e=r.clear();o.clear(),a&&k(n,function(){e.forEach(function(e){O(n,e.value)})})},t.getSize=r.getSize,t.getValueType=function(){return n},t.getMaxSize=r.getMaxSize,t.setMaxSize=function(e){var t=r.setMaxSize(e);k(n,function(){t.forEach(function(e){e.value.alternateKeys.forEach(function(e){o.delete(e)}),u&&j(n,e.value)})})},t}var P=new Map,K=function(e){var t=P.get(e);return void 0===t&&(t=C(e),P.set(e,t)),t},L=function(){S(function(){P.forEach(function(e){e.clear()})})}}]);
module.exports=function(t){var r={};function u(e){if(r[e])return r[e].exports;var n=r[e]={i:e,l:!1,exports:{}};return t[e].call(n.exports,n,n.exports,u),n.l=!0,n.exports}return u.m=t,u.c=r,u.d=function(e,n,t){u.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:t})},u.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},u.t=function(n,e){if(1&e&&(n=u(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var t=Object.create(null);if(u.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var r in n)u.d(t,r,function(e){return n[e]}.bind(null,r));return t},u.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return u.d(n,"a",n),n},u.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},u.p="",u(u.s=0)}([function(e,n,t){"use strict";t.r(n);var l=function(e,n){null===n.newest?(n.newest=e,n.oldest=e):((n.newest.next=e).prev=n.newest,n.newest=e)},i=function(e,n){e!==n.newest&&(null===(n.newest.next=e).prev?(e.prev=n.newest,e.next.prev=null,n.oldest=e.next):(e.prev.next=e.next,e.next.prev=e.prev,e.prev=n.newest),e.next=null,n.newest=e)},r=function(e,n,t){null===e.next?n.newest=e.prev:e.next.prev=e.prev,null===e.prev?n.oldest=e.next:e.prev.next=e.next,t.delete(e.key)},c=function(e,n,t){var r,u,o,a=[];if(null===e)return a;for(;t.size>e;)a.push((u=t,void 0,o=(r=n).oldest,u.delete(r.oldest.key),r.oldest=r.oldest.next,r.oldest.prev=null,{key:o.key,value:o.value}));return a};function f(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,n=this instanceof f?this:Object.create(f.prototype),u=e;0===u&&(u=null);var o=new Map,a={newest:null,oldest:null};return n.setMaxSize=function(e){return(u=e)<1&&(u=null),c(u,a,o)},n.getMaxSize=function(){return u},n.getSize=function(){return o.size},n.set=function(e,n){var t=o.get(e);if(void 0!==t)return t.value=n,i(t,a),null;t={key:e,value:n,next:null,prev:null},o.set(e,t),l(t,a);var r=c(u,a,o);return 1===r.length?r[0]:null},n.get=function(e){var n=o.get(e);return void 0===n?n:(i(n,a),n.value)},n.getWithoutLruChange=function(e){var n=o.get(e);return void 0===n?n:n.value},n.delete=function(e){var n=o.get(e);return void 0!==n&&(r(n,a,o),!0)},n.forEach=function(e){for(var n=a.oldest;null!==n;)e(n.value,n.key),n=n.next},n.map=function(e){for(var n=a.oldest,t=[];null!==n;)t.push(e(n.value,n.key)),n=n.next;return t},n.clear=function(){var e=n.map(function(e,n){return{key:n,value:e}});return o.clear(),a.newest=null,a.oldest=null,e},n}function d(e){return function(e){if(Array.isArray(e)){for(var n=0,t=new Array(e.length);n<e.length;n++)t[n]=e[n];return t}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function a(n){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{},r=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(t).filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.forEach(function(e){s(n,e,t[e])})}return n}function s(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}t.d(n,"registerCacheChangedHandler",function(){return w}),t.d(n,"cacheTransaction",function(){return S}),t.d(n,"getCache",function(){return L}),t.d(n,"clearAllCaches",function(){return _});var v=500,u=0,y=new Map,h=new Map,p=new Set,o=function(t,e){null===e||Array.isArray(e)&&0===e.length?p.add(t):(Array.isArray(e)?e:[e]).forEach(function(e){var n=h.get(e);void 0===n&&(n=new Set,h.set(e,n)),n.add(t)})},g=function(n,e){null===e||Array.isArray(e)&&0===e.length?p.delete(n):(Array.isArray(e)?e:[e]).forEach(function(e){h.get(e).delete(n)})},w=function(e){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,t=u;u+=1;var r={changedHandler:e,valueTypes:n,isActive:!0,unregister:function(){y.delete(t),g(t,n)},activate:function(){r.isActive=!0,o(t,n)},deactivate:function(){r.isActive=!1,g(t,n)},isRegistered:function(){return y.has(t)}};return y.set(t,r),o(t,n),r},m=function(t){var e,n,r,u=(e=t.valueTypes,n="string"==typeof e?[e]:e,r=new Set,n.forEach(function(e){var n=h.get(e);void 0!==n&&n.forEach(function(e){r.add(e)})}),p.forEach(function(e){r.add(e)}),r),o=[],a=0;if(u.forEach(function(e){var n=y.get(e);try{n.changedHandler(t)}catch(e){o.push(e)}finally{a+=1}}),0<o.length){var l="handleTransactionChangeObject: "+String(o.length)+" of "+String(a)+" handlers threw an error: ";o.forEach(function(e){l+=e.message+", "});var i=new Error(l);throw i.errors=o,i}},b=null,A=0,x=0,S=function(e){if(null===b&&(b={valueTypes:new Set}),x+=1,"function"==typeof e.finally)e.finally(function(){if(0===(x-=1))try{m(b)}finally{b=null,A=0}});else try{e()}finally{if(0===(x-=1))try{m(b)}finally{b=null,A=0}}},E=function(n,e,t,r){var u=b,o=null!==u;null===u&&(u={valueTypes:new Set}),u.valueTypes.has(n)?u[n][t].push(a({},e,{order:A++})):(u.valueTypes.add(n),u[n]=s({},t,[a({},e,{order:A++})]),r.forEach(function(e){u[n][e]=[]})),o||m(u)},O=!0,j=function(e,n){O&&E(e,n,"clearRemoves",["inserts","lruRemoves","deleteRemoves"])},k=function(e,n){O&&E(e,n,"lruRemoves",["clearRemoves","inserts","deleteRemoves"])},M=function(e,n){O&&E(e,n,"deleteRemoves",["clearRemoves","lruRemoves","inserts"])},R=function(u){return function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return new Promise(function(n,t){setTimeout(function(){try{var e=u.apply(void 0,r);n(e)}catch(e){t(e)}},0)})}},z=function(e,n){if(function(e){if(0<p.size)return!0;var n=h.get(e);return void 0!==n&&0<n.size}(e))S(n);else try{O=!1,n()}finally{O=!0}},C=function(c,f,s,e,v){if(!Array.isArray(e))throw new Error("LruCache::setAll: keyValueAlternateKeysArray must be an array");z(c,function(){e.forEach(function(e){var n=e.key,t=e.value,r=e.alternateKeys,u=f.getWithoutLruChange(n),o=Array.isArray(r)?r:[];0===o.length&&"string"==typeof r&&(o=[r]),o.forEach(function(e){if(s.has(e)&&s.get(e)!==n)throw new Error("LruCache::setAll: alternate key '"+e+"' is given for key '"+n+"' and value type '"+c+"' but is already used for key '"+s.get(e)+"'")}),o=new Set(o),void 0===u?u={key:n,value:t,alternateKeys:o}:(u.value=t,u.alternateKeys=new Set(d(u.alternateKeys).concat(d(o))));var a,l,i=f.set(n,u);o.forEach(function(e){s.set(e,n)}),a=c,l=u,O&&E(a,l,"inserts",["clearRemoves","lruRemoves","deleteRemoves"]),null!==i&&(i.value.alternateKeys.forEach(function(e){s.delete(e)}),v&&k(c,i.value))})})},T=function(e,n,t){var r=t(e);return void 0===r&&n.has(e)&&(r=t(n.get(e))),r};function P(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:v,n=this instanceof P?this:Object.create(P.prototype),r=f(e),u=new Map,o=!1,a=!1;return n.dispatchLruRemoves=function(e){o=e},n.dispatchClearRemoves=function(e){a=e},n.setAll=function(e){C(t,r,u,e,o)},n.setAllAsync=R(n.setAll),n.set=function(e){n.setAll([e])},n.setAsync=R(n.set),n.get=function(e){var n=T(e,u,r.get);return void 0===n?n:n.value},n.getWithoutLruChange=function(e){var n=T(e,u,r.getWithoutLruChange);return void 0===n?n:n.value},n.delete=function(e){var n=T(e,u,r.getWithoutLruChange);return void 0!==n&&(r.delete(n.key),n.alternateKeys.forEach(function(e){u.delete(e)}),z(t,function(){M(t,n)}),!0)},n.forEach=r.forEach,n.getEntries=function(){return r.map(function(e){return e})},n.clear=function(){var e=r.clear();u.clear(),a&&z(t,function(){e.forEach(function(e){j(t,e.value)})})},n.getSize=r.getSize,n.getValueType=function(){return t},n.getMaxSize=r.getMaxSize,n.setMaxSize=function(e){var n=r.setMaxSize(e);z(t,function(){n.forEach(function(e){e.value.alternateKeys.forEach(function(e){u.delete(e)}),o&&k(t,e.value)})})},n}var K=new Map,L=function(e){var n=K.get(e);return void 0===n&&(n=P(e),K.set(e,n)),n},_=function(){S(function(){K.forEach(function(e){e.clear()})})}}]);
{
"name": "@swarmy/lru-cache",
"version": "2.0.3",
"version": "2.0.4",
"description": "LRU cache with event registry to handle cache changes",

@@ -5,0 +5,0 @@ "author": "Gerd Neudert",

@@ -37,4 +37,5 @@ # lru-cache

if (!user) {
await fetchUser(userId);
user = await fetchUser(userId);
}
...
```

@@ -84,7 +85,6 @@ * For the same value type (here "User"), getCache will always return the same cache instance.

## Questions
### What are the tradeoffs?
## Performance
* Compared to a native Javascript Map, the LRU logic implies performance impact on get, set and delete. It's just the price to pay for having a LRU cache.
* See [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/a58e345708cb07d4f24434eba9ea4760d61a264b/performance-report.html)
* However, the methods are still O(1). (Only setMaxSize has O(size-newMaxSize), if size>newMaxSize)
* Compared to a LRU cache without cache events, the is additional performance impact on get, set and delete.

@@ -95,2 +95,3 @@ * Again see [Performance tests](https://rawcdn.githack.com/gneu77/lru-cache/a58e345708cb07d4f24434eba9ea4760d61a264b/performance-report.html)

## Questions

@@ -97,0 +98,0 @@ ### Why can I not differentiate between insert and update in the change events?

@@ -285,3 +285,3 @@ import {LruMap} from "./LruMap";

keyValueAlternateKeysArray.forEach(({key, value, alternateKeys}) => {
let entry = lruMap.get(key);
let entry = lruMap.getWithoutLruChange(key);
let altKeys = Array.isArray(alternateKeys) ? alternateKeys : [];

@@ -288,0 +288,0 @@ if (altKeys.length === 0 && typeof alternateKeys === "string") {

/* eslint-disable no-param-reassign */
const addNewest = (entry, entryList) => {
if (entryList.newest === null) {
entryList.newest = entry;
entryList.oldest = entry;
}
else {
entryList.newest.next = entry;
entry.prev = entryList.newest;
entryList.newest = entry;
}
};
/* eslint-disable no-param-reassign */
const makeNewest = (entry, entryList) => {

@@ -8,27 +21,18 @@ if (entry === entryList.newest) {

}
if (entryList.newest === null) {
// first entry
entryList.newest = entry;
entryList.oldest = entry;
return;
entryList.newest.next = entry;
if (entry.prev === null) {
// entry is current oldest (and not newest)
entry.prev = entryList.newest;
entry.next.prev = null;
entryList.oldest = entry.next;
entry.next = null;
}
const eNext = entry.next;
const ePrev = entry.prev;
const oldNewest = entryList.newest;
entry.next = null;
entry.prev = entryList.newest;
else {
// entry is neither newest, nor oldest
entry.prev.next = entry.next;
entry.next.prev = entry.prev;
entry.prev = entryList.newest;
entry.next = null;
}
entryList.newest = entry;
if (eNext !== null) {
eNext.prev = ePrev;
if (entryList.oldest === entry) {
entryList.oldest = eNext;
}
}
if (oldNewest !== null) {
oldNewest.next = entry;
entry.prev = oldNewest;
}
if (ePrev !== null) {
ePrev.next = eNext;
}
};

@@ -54,12 +58,9 @@

const removeOldest = (entryList, keyToEntry) => {
// As maxSize cannot be <1 and removeOldest is only called, if maxSize exceeded,
// we can be sure that at least two entries exist.
const removedEntry = entryList.oldest;
if (keyToEntry.delete(entryList.oldest.key)) {
entryList.oldest = entryList.oldest.next;
if (entryList.oldest !== null) {
entryList.oldest.prev = null;
}
}
delete removedEntry.next;
delete removedEntry.prev;
return removedEntry;
keyToEntry.delete(entryList.oldest.key);
entryList.oldest = entryList.oldest.next;
entryList.oldest.prev = null;
return {key: removedEntry.key, value: removedEntry.value};
};

@@ -136,9 +137,11 @@

keyToEntry.set(key, entry);
addNewest(entry, entryList);
const removals = shrinkToMaxSize(sizeLimit, entryList, keyToEntry);
return removals.length === 1 ? removals[0] : null;
}
else {
entry.value = value;
makeNewest(entry, entryList);
return null;
}
makeNewest(entry, entryList);
const removals = shrinkToMaxSize(sizeLimit, entryList, keyToEntry);
return removals.length === 1 ? removals[0] : null;
};

@@ -164,8 +167,5 @@

*/
self.getWithoutLruChange = key => { // eslint-disable-line consistent-return
// Using the try-catch approach here is significantly faster compared to prior testing if the key is in the map.
try {
return keyToEntry.get(key).value;
}
catch (e) {} // eslint-disable-line no-empty
self.getWithoutLruChange = key => {
const entry = keyToEntry.get(key);
return typeof entry === "undefined" ? entry : entry.value;
};

@@ -172,0 +172,0 @@

@@ -160,4 +160,11 @@ /* eslint-disable no-magic-number */

expect(keys[1]).toEqual("key5");
lruMap.set("key6", "value6");
lruMap.get("key5");
keys = lruMap.map((value, key) => key);
expect(keys[0]).toEqual("key4");
expect(keys[1]).toEqual("key6");
expect(keys[2]).toEqual("key5");
});
});

@@ -1,2 +0,2 @@

/* eslint-disable no-magic-number */
/* eslint-disable no-magic-number, no-unused-expressions */

@@ -19,2 +19,3 @@ import {LruMap} from "../LruMap";

const VALUE_TYPE = "PT";
const VALUE_TYPE2 = "PT2";

@@ -35,2 +36,32 @@ const getRandomString = () => Math.random().toString(36).substr(2, 10);

describe("Performance Javascript Object", () => {
const map = {};
it("should measure the time for " + nData + " inserts and " + nUpdates + " updates in a normal Javascript Object", () => {
for (let n = 0; n < nLoops; ++n) {
for (let i = 0; i < nData; ++i) {
map[testKeys[i]] = testValues[i];
}
for (let i = 0, j = nData - 1; i < nData; ++i, --j) {
map[testKeys[i]] = testValues[j];
}
}
});
it("should measure the time for " + nGets + " by-key-accesses on normal Javascript Object", () => {
for (let n = 0; n < nGetLoops; ++n) {
for (let i = 0; i < nData; ++i) {
map[testKeys[i]];
}
}
});
it("should measure the time for " + nData + " deletes from Javascript Object", () => {
for (let i = 0; i < nData; ++i) {
delete map[testKeys[i]];
}
});
});
describe("Performance Javascript Map", () => {

@@ -58,2 +89,8 @@

it("should measure the time for " + nData + " 'delete' calls on normal Javascript Map", () => {
for (let i = 0; i < nData; ++i) {
map.delete(testKeys[i]);
}
});
});

@@ -93,2 +130,10 @@

it("should measure the time for " + nData + " 'delete' calls on LruMap", () => {
for (let n = 0; n < nGetLoops; ++n) {
for (let i = 0; i < nData; ++i) {
map1.delete(testKeys[i]);
}
}
});
it("should measure the time for " + nData + " inserts and " + nUpdates + " updates in LruMap with maxSize 3", () => {

@@ -111,2 +156,13 @@ const map = new LruMap(3);

beforeAll(() => {
const cache = getCache(VALUE_TYPE2);
cache.setMaxSize(null);
for (let i = 0; i < nData; ++i) {
cache.set({
key: testKeys[i],
value: testValues[i],
});
}
});
afterEach(() => {

@@ -122,6 +178,12 @@ // We do this in afterEach, because we don't want to measure the time for the clear

for (let i = 0; i < nData; ++i) {
cache.set(testKeys[i], testValues[i]);
cache.set({
key: testKeys[i],
value: testValues[i],
});
}
for (let i = 0, j = nData - 1; i < nData; ++i, --j) {
cache.set(testKeys[i], testValues[j]);
cache.set({
key: testKeys[i],
value: testValues[j],
});
}

@@ -131,2 +193,11 @@ }

it("should measure the time for " + nGets + " 'get' calls on LruCache (should not be much slower compared to LruMap)", () => {
const cache = getCache(VALUE_TYPE2);
for (let n = 0; n < nGetLoops; ++n) {
for (let i = 0; i < nData; ++i) {
cache.get(testKeys[i]);
}
}
});
it("should measure the time for " + nData + " inserts and " + nUpdates + " updates in LruCache with maxSize 3 and no event listener", () => {

@@ -137,6 +208,12 @@ const cache = getCache(VALUE_TYPE);

for (let i = 0; i < nData; ++i) {
cache.set(testKeys[i], testValues[i]);
cache.set({
key: testKeys[i],
value: testValues[i],
});
}
for (let i = 0, j = nData - 1; i < nData; ++i, --j) {
cache.set(testKeys[i], testValues[j]);
cache.set({
key: testKeys[i],
value: testValues[j],
});
}

@@ -147,2 +224,1 @@ }

});
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