Update Bot
This commit is contained in:
44
node_modules/lru.min/README.md
generated
vendored
44
node_modules/lru.min/README.md
generated
vendored
@@ -346,15 +346,18 @@ LRU.set(2, { name: 'Mary' });
|
||||
The benchmark is performed by comparing `1,000,000` runs through a maximum cache limit of `100,000`, getting `333,333` caches and deleting `200,000` keys 10 consecutive times, clearing the cache every run.
|
||||
|
||||
> - [**lru-cache**](https://github.com/isaacs/node-lru-cache) `v11.0.0`
|
||||
> - [**quick-lru**](https://github.com/sindresorhus/quick-lru) `v7.0.0`
|
||||
|
||||
```sh
|
||||
# Time:
|
||||
lru.min: 240.45ms
|
||||
lru-cache: 258.32ms
|
||||
quick-lru: 279.89ms
|
||||
|
||||
# CPU:
|
||||
lru.min: 275558.30µs
|
||||
lru-cache: 306858.30µs
|
||||
quick-lru: 401318.80µs
|
||||
```
|
||||
|
||||
- See detailed results and how the tests are run and compared in the [**benchmark**](https://github.com/wellwelwel/lru.min/tree/main/benchmark) directory.
|
||||
@@ -377,12 +380,43 @@ See the [**Contributing Guide**](https://github.com/wellwelwel/lru.min/blob/main
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
- [](https://github.com/wellwelwel/lru.min/graphs/contributors)
|
||||
- **lru.min** is inspired by [**lru-cache**](https://github.com/isaacs/node-lru-cache) architecture and [**quick-lru**](https://github.com/sindresorhus/quick-lru) usage, simplifying and improving their concepts for enhanced performance and compatibility.
|
||||
**lru.min** is based and inspired on the architecture and code of both [**lru-cache**](https://github.com/isaacs/node-lru-cache) and [**quick-lru**](https://github.com/sindresorhus/quick-lru), simplifying their core concepts for enhanced performance and compatibility.
|
||||
|
||||
> [!IMPORTANT]
|
||||
>
|
||||
> No [**lru-cache**](https://github.com/isaacs/node-lru-cache) or [**quick-lru**](https://github.com/sindresorhus/quick-lru) code is used in **lru.min**. For more comprehensive features such as **TTL** support, consider using and supporting them 🤝
|
||||
For more comprehensive features such as **TTL** support, consider using and supporting them 🤝
|
||||
|
||||
- The architecture is mostly based on [@isaacs](https://github.com/isaacs) — [**lru-cache**](https://github.com/isaacs/node-lru-cache/blob/8f51d75351cbb4ac819952eb8e9f95eda00ef800/src/index.ts).
|
||||
- Most of the methods names and its functionalities were inspired by [@sindresorhus](https://github.com/sindresorhus) — [**quick-lru**](https://github.com/sindresorhus/quick-lru/blob/a2262c65e1952539cb4d985a67c46363a780d234/index.js).
|
||||
- [](https://github.com/wellwelwel/lru.min/graphs/contributors)
|
||||
|
||||
---
|
||||
|
||||
#### What comes from [**lru-cache**](https://github.com/isaacs/node-lru-cache)?
|
||||
|
||||
Architecture's essence:
|
||||
|
||||
> _It's not the same code, but majority based on [this](https://github.com/isaacs/node-lru-cache/blob/8f51d75351cbb4ac819952eb8e9f95eda00ef800/src/index.ts#L1385-L1394)._
|
||||
|
||||
```ts
|
||||
let free: number[] = [];
|
||||
|
||||
const keyMap: Map<Key, number> = new Map();
|
||||
const keyList: (Key | undefined)[] = new Array(max).fill(undefined);
|
||||
const valList: (Value | undefined)[] = new Array(max).fill(undefined);
|
||||
const next: number[] = new Array(max).fill(0);
|
||||
const prev: number[] = new Array(max).fill(0);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### What comes from [**quick-lru**](https://github.com/sindresorhus/quick-lru)?
|
||||
|
||||
Name of methods and options _(including their final functionality ideas)_:
|
||||
|
||||
- `resize`
|
||||
- `peek`
|
||||
- `onEviction`
|
||||
- `forEach`
|
||||
- `entriesDescending` as `entries`
|
||||
|
||||
---
|
||||
|
||||
|
||||
2
node_modules/lru.min/browser/lru.min.js
generated
vendored
2
node_modules/lru.min/browser/lru.min.js
generated
vendored
@@ -1 +1 @@
|
||||
"use strict";window.createLRU=function(e){var r=e.max;if(!(Number.isInteger(r)&&r>0))throw new TypeError("`max` must be a positive integer");var n=0,i=0,t=0,l=[],o=e.onEviction,a=new Map,f=new Array(r).fill(void 0),v=new Array(r).fill(void 0),u=new Array(r).fill(0),d=new Array(r).fill(0),c=function(e){u[t]=e,d[e]=t,u[e]=0,t=e},s=function(e){if(e!==t){var r=u[e],n=d[e];e===i?i=r:u[n]=r,d[r]=n,c(e)}};return{set:function(e,g){if(void 0!==e){var h=a.get(e);if(void 0===h){if(n===r){var w=f[h=i];null==o||o(w,v[h]),a.delete(w),i=u[h],d[i]=0}else h=l.length>0?l.pop():n,n++;a.set(e,h),f[h]=e,v[h]=g,1===n?i=t=h:c(h)}else null==o||o(e,v[h]),v[h]=g,s(h)}},get:function(e){var r=a.get(e);if(void 0!==r)return r!==t&&s(r),v[r]},peek:function(e){var r=a.get(e);return void 0!==r?v[r]:void 0},has:function(e){return a.has(e)},keys:function*(){for(var e=t,r=0;r<n;r++)yield f[e],e=d[e]},values:function*(){for(var e=t,r=0;r<n;r++)yield v[e],e=d[e]},entries:function*(){for(var e=t,r=0;r<n;r++)yield[f[e],v[e]],e=d[e]},forEach:function(e){for(var r=t,i=0;i<n;i++){var l=f[r];e(v[r],l),r=d[r]}},delete:function(e){var r=a.get(e);if(void 0===r)return!1;null==o||o(e,v[r]),a.delete(e),l.push(r),f[r]=void 0,v[r]=void 0;var c=d[r],s=u[r];return r===i?i=s:u[c]=s,r===t?t=c:d[s]=c,n--,!0},evict:function(e){for(var r=Math.min(e,n);r>0;){var c=i,s=f[c];null==o||o(s,v[c]),a.delete(s),f[c]=void 0,v[c]=void 0,i=u[c],d[i]=0,n--,l.push(c),r--}0===n&&(i=t=0)},clear:function(){if(o)for(var e=i,r=0;r<n;r++)o(f[e],v[e]),e=u[e];a.clear(),f.fill(void 0),v.fill(void 0),l=[],n=0,i=t=0},resize:function(e){if(!(Number.isInteger(e)&&e>0))throw new TypeError("`max` must be a positive integer");e!==r&&(e<r?function(e){for(var r=t,c=Math.min(n,e),s=n-c,g=new Array(c),h=new Array(c),w=0;w<s;w++){var y=f[i];null==o||o(y,v[i]),a.delete(y),i=u[i]}for(var p=c-1;p>=0;p--)g[p]=f[r],h[p]=v[r],a.set(f[r],p),r=d[r];i=0,t=c-1,n=c,f.length=e,v.length=e,u.length=e,d.length=e;for(var m=0;m<c;m++)f[m]=g[m],v[m]=h[m],u[m]=m+1,d[m]=m-1;l=[];for(var A=c;A<e;A++)l.push(A)}(e):function(e){f.length=e,v.length=e,u.length=e,d.length=e,f.fill(void 0,r),v.fill(void 0,r),u.fill(0,r),d.fill(0,r)}(e),r=e)},get max(){return r},get size(){return n},get available(){return r-n}}};
|
||||
"use strict";window.createLRU=function(e){var r=e.max;if(!(Number.isInteger(r)&&r>0))throw new TypeError("`max` must be a positive integer");var n=0,i=0,t=0,a=[],o=e.onEviction,l=new Map,f=new Array(r).fill(void 0),u=new Array(r).fill(void 0),v=new Array(r).fill(0),s=new Array(r).fill(0),d=function(e,r){if(e!==t){var n=v[e],a=s[e];e===i?i=n:("get"===r||0!==a)&&(v[a]=n),0!==n&&(s[n]=a),v[t]=e,s[e]=t,v[e]=0,t=e}},p=function(){var e=i,r=f[e];return null==o||o(r,u[e]),l.delete(r),f[e]=void 0,u[e]=void 0,0!==(i=v[e])&&(s[i]=0),0===--n&&(i=t=0),a.push(e),e};return{set:function(e,v){if(void 0!==e){var s=l.get(e);void 0===s?(s=n===r?p():a.length>0?a.pop():n,l.set(e,s),f[s]=e,n++):null==o||o(e,u[s]),u[s]=v,1===n?i=t=s:d(s,"set")}},get:function(e){var r=l.get(e);if(void 0!==r)return r!==t&&d(r,"get"),u[r]},peek:function(e){var r=l.get(e);return void 0!==r?u[r]:void 0},has:function(e){return l.has(e)},keys:function*(){for(var e=t,r=0;r<n;r++)yield f[e],e=s[e]},values:function*(){for(var e=t,r=0;r<n;r++)yield u[e],e=s[e]},entries:function*(){for(var e=t,r=0;r<n;r++)yield[f[e],u[e]],e=s[e]},forEach:function(e){for(var r=t,i=0;i<n;i++){var a=f[r];e(u[r],a),r=s[r]}},delete:function(e){var r=l.get(e);if(void 0===r)return!1;null==o||o(e,u[r]),l.delete(e),a.push(r),f[r]=void 0,u[r]=void 0;var d=s[r],p=v[r];return 0!==d&&(v[d]=p),0!==p&&(s[p]=d),r===i&&(i=p),r===t&&(t=d),n--,!0},evict:function(e){for(var r=Math.min(e,n);r>0;)p(),r--},clear:function(){if("function"==typeof o)for(var e=l.values(),r=e.next();!r.done;r=e.next())o(f[r.value],u[r.value]);l.clear(),f.fill(void 0),u.fill(void 0),a=[],n=0,i=t=0},resize:function(e){if(!(Number.isInteger(e)&&e>0))throw new TypeError("`max` must be a positive integer");if(e!==r){if(e<r){for(var d=t,p=Math.min(n,e),c=n-p,y=new Array(e),g=new Array(e),h=new Array(e),w=new Array(e),A=1;A<=c;A++)null==o||o(f[A],u[A]);for(var m=p-1;m>=0;m--)y[m]=f[d],g[m]=u[d],h[m]=m+1,w[m]=m-1,l.set(y[m],m),d=s[d];i=0,t=p-1,n=p,f.length=e,u.length=e,v.length=e,s.length=e;for(var x=0;x<p;x++)f[x]=y[x],u[x]=g[x],v[x]=h[x],s[x]=w[x];a=[];for(var b=p;b<e;b++)a.push(b)}else{var E=e-r;f.push.apply(f,new Array(E).fill(void 0)),u.push.apply(u,new Array(E).fill(void 0)),v.push.apply(v,new Array(E).fill(0)),s.push.apply(s,new Array(E).fill(0))}r=e}},get max(){return r},get size(){return n},get available(){return r-n}}};
|
||||
185
node_modules/lru.min/lib/index.js
generated
vendored
185
node_modules/lru.min/lib/index.js
generated
vendored
@@ -15,68 +15,37 @@ const createLRU = (options) => {
|
||||
const valList = new Array(max).fill(undefined);
|
||||
const next = new Array(max).fill(0);
|
||||
const prev = new Array(max).fill(0);
|
||||
const linkTail = (index) => {
|
||||
next[tail] = index;
|
||||
prev[index] = tail;
|
||||
next[index] = 0;
|
||||
tail = index;
|
||||
};
|
||||
const moveToTail = (index) => {
|
||||
const setTail = (index, type) => {
|
||||
if (index === tail)
|
||||
return;
|
||||
const nextIndex = next[index];
|
||||
const prevIndex = prev[index];
|
||||
if (index === head)
|
||||
head = nextIndex;
|
||||
else
|
||||
else if (type === 'get' || prevIndex !== 0)
|
||||
next[prevIndex] = nextIndex;
|
||||
prev[nextIndex] = prevIndex;
|
||||
linkTail(index);
|
||||
if (nextIndex !== 0)
|
||||
prev[nextIndex] = prevIndex;
|
||||
next[tail] = index;
|
||||
prev[index] = tail;
|
||||
next[index] = 0;
|
||||
tail = index;
|
||||
};
|
||||
const _shrink = (newMax) => {
|
||||
let current = tail;
|
||||
const preserve = Math.min(size, newMax);
|
||||
const remove = size - preserve;
|
||||
const newKeyList = new Array(preserve);
|
||||
const newValList = new Array(preserve);
|
||||
for (let i = 0; i < remove; i++) {
|
||||
const key = keyList[head];
|
||||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(key, valList[head]);
|
||||
keyMap.delete(key);
|
||||
head = next[head];
|
||||
}
|
||||
for (let i = preserve - 1; i >= 0; i--) {
|
||||
newKeyList[i] = keyList[current];
|
||||
newValList[i] = valList[current];
|
||||
keyMap.set(keyList[current], i);
|
||||
current = prev[current];
|
||||
}
|
||||
head = 0;
|
||||
tail = preserve - 1;
|
||||
size = preserve;
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
for (let i = 0; i < preserve; i++) {
|
||||
keyList[i] = newKeyList[i];
|
||||
valList[i] = newValList[i];
|
||||
next[i] = i + 1;
|
||||
prev[i] = i - 1;
|
||||
}
|
||||
free = [];
|
||||
for (let i = preserve; i < newMax; i++)
|
||||
free.push(i);
|
||||
};
|
||||
const _grow = (newMax) => {
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
keyList.fill(undefined, max);
|
||||
valList.fill(undefined, max);
|
||||
next.fill(0, max);
|
||||
prev.fill(0, max);
|
||||
const _evict = () => {
|
||||
const evictHead = head;
|
||||
const key = keyList[evictHead];
|
||||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(key, valList[evictHead]);
|
||||
keyMap.delete(key);
|
||||
keyList[evictHead] = undefined;
|
||||
valList[evictHead] = undefined;
|
||||
head = next[evictHead];
|
||||
if (head !== 0)
|
||||
prev[head] = 0;
|
||||
size--;
|
||||
if (size === 0)
|
||||
head = tail = 0;
|
||||
free.push(evictHead);
|
||||
return evictHead;
|
||||
};
|
||||
return {
|
||||
/** Adds a key-value pair to the cache. Updates the value if the key already exists. */
|
||||
@@ -85,31 +54,18 @@ const createLRU = (options) => {
|
||||
return;
|
||||
let index = keyMap.get(key);
|
||||
if (index === undefined) {
|
||||
if (size === max) {
|
||||
index = head;
|
||||
const evictKey = keyList[index];
|
||||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(evictKey, valList[index]);
|
||||
keyMap.delete(evictKey);
|
||||
head = next[index];
|
||||
prev[head] = 0;
|
||||
}
|
||||
else {
|
||||
index = free.length > 0 ? free.pop() : size;
|
||||
size++;
|
||||
}
|
||||
index = size === max ? _evict() : free.length > 0 ? free.pop() : size;
|
||||
keyMap.set(key, index);
|
||||
keyList[index] = key;
|
||||
valList[index] = value;
|
||||
if (size === 1)
|
||||
head = tail = index;
|
||||
else
|
||||
linkTail(index);
|
||||
size++;
|
||||
}
|
||||
else {
|
||||
else
|
||||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(key, valList[index]);
|
||||
valList[index] = value;
|
||||
moveToTail(index);
|
||||
}
|
||||
valList[index] = value;
|
||||
if (size === 1)
|
||||
head = tail = index;
|
||||
else
|
||||
setTail(index, 'set');
|
||||
},
|
||||
/** Retrieves the value for a given key and moves the key to the most recent position. */
|
||||
get(key) {
|
||||
@@ -117,7 +73,7 @@ const createLRU = (options) => {
|
||||
if (index === undefined)
|
||||
return;
|
||||
if (index !== tail)
|
||||
moveToTail(index);
|
||||
setTail(index, 'get');
|
||||
return valList[index];
|
||||
},
|
||||
/** Retrieves the value for a given key without changing its position. */
|
||||
@@ -173,14 +129,14 @@ const createLRU = (options) => {
|
||||
valList[index] = undefined;
|
||||
const prevIndex = prev[index];
|
||||
const nextIndex = next[index];
|
||||
if (prevIndex !== 0)
|
||||
next[prevIndex] = nextIndex;
|
||||
if (nextIndex !== 0)
|
||||
prev[nextIndex] = prevIndex;
|
||||
if (index === head)
|
||||
head = nextIndex;
|
||||
else
|
||||
next[prevIndex] = nextIndex;
|
||||
if (index === tail)
|
||||
tail = prevIndex;
|
||||
else
|
||||
prev[nextIndex] = prevIndex;
|
||||
size--;
|
||||
return true;
|
||||
},
|
||||
@@ -188,29 +144,16 @@ const createLRU = (options) => {
|
||||
evict: (number) => {
|
||||
let toPrune = Math.min(number, size);
|
||||
while (toPrune > 0) {
|
||||
const evictHead = head;
|
||||
const key = keyList[evictHead];
|
||||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(key, valList[evictHead]);
|
||||
keyMap.delete(key);
|
||||
keyList[evictHead] = undefined;
|
||||
valList[evictHead] = undefined;
|
||||
head = next[evictHead];
|
||||
prev[head] = 0;
|
||||
size--;
|
||||
free.push(evictHead);
|
||||
_evict();
|
||||
toPrune--;
|
||||
}
|
||||
if (size === 0)
|
||||
head = tail = 0;
|
||||
},
|
||||
/** Clears all key-value pairs from the cache. */
|
||||
clear() {
|
||||
if (onEviction) {
|
||||
let current = head;
|
||||
for (let i = 0; i < size; i++) {
|
||||
onEviction(keyList[current], valList[current]);
|
||||
current = next[current];
|
||||
}
|
||||
if (typeof onEviction === 'function') {
|
||||
const iterator = keyMap.values();
|
||||
for (let result = iterator.next(); !result.done; result = iterator.next())
|
||||
onEviction(keyList[result.value], valList[result.value]);
|
||||
}
|
||||
keyMap.clear();
|
||||
keyList.fill(undefined);
|
||||
@@ -225,10 +168,48 @@ const createLRU = (options) => {
|
||||
throw new TypeError('`max` must be a positive integer');
|
||||
if (newMax === max)
|
||||
return;
|
||||
if (newMax < max)
|
||||
_shrink(newMax);
|
||||
else
|
||||
_grow(newMax);
|
||||
if (newMax < max) {
|
||||
let current = tail;
|
||||
const preserve = Math.min(size, newMax);
|
||||
const remove = size - preserve;
|
||||
const newKeyList = new Array(newMax);
|
||||
const newValList = new Array(newMax);
|
||||
const newNext = new Array(newMax);
|
||||
const newPrev = new Array(newMax);
|
||||
for (let i = 1; i <= remove; i++)
|
||||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(keyList[i], valList[i]);
|
||||
for (let i = preserve - 1; i >= 0; i--) {
|
||||
newKeyList[i] = keyList[current];
|
||||
newValList[i] = valList[current];
|
||||
newNext[i] = i + 1;
|
||||
newPrev[i] = i - 1;
|
||||
keyMap.set(newKeyList[i], i);
|
||||
current = prev[current];
|
||||
}
|
||||
head = 0;
|
||||
tail = preserve - 1;
|
||||
size = preserve;
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
for (let i = 0; i < preserve; i++) {
|
||||
keyList[i] = newKeyList[i];
|
||||
valList[i] = newValList[i];
|
||||
next[i] = newNext[i];
|
||||
prev[i] = newPrev[i];
|
||||
}
|
||||
free = [];
|
||||
for (let i = preserve; i < newMax; i++)
|
||||
free.push(i);
|
||||
}
|
||||
else {
|
||||
const fill = newMax - max;
|
||||
keyList.push(...new Array(fill).fill(undefined));
|
||||
valList.push(...new Array(fill).fill(undefined));
|
||||
next.push(...new Array(fill).fill(0));
|
||||
prev.push(...new Array(fill).fill(0));
|
||||
}
|
||||
max = newMax;
|
||||
},
|
||||
/** Returns the maximum number of items that can be stored in the cache. */
|
||||
|
||||
168
node_modules/lru.min/lib/index.mjs
generated
vendored
168
node_modules/lru.min/lib/index.mjs
generated
vendored
@@ -12,64 +12,31 @@ const createLRU = (options) => {
|
||||
const valList = new Array(max).fill(void 0);
|
||||
const next = new Array(max).fill(0);
|
||||
const prev = new Array(max).fill(0);
|
||||
const linkTail = (index) => {
|
||||
const setTail = (index, type) => {
|
||||
if (index === tail) return;
|
||||
const nextIndex = next[index];
|
||||
const prevIndex = prev[index];
|
||||
if (index === head) head = nextIndex;
|
||||
else if (type === "get" || prevIndex !== 0) next[prevIndex] = nextIndex;
|
||||
if (nextIndex !== 0) prev[nextIndex] = prevIndex;
|
||||
next[tail] = index;
|
||||
prev[index] = tail;
|
||||
next[index] = 0;
|
||||
tail = index;
|
||||
};
|
||||
const moveToTail = (index) => {
|
||||
if (index === tail) return;
|
||||
const nextIndex = next[index];
|
||||
const prevIndex = prev[index];
|
||||
if (index === head) head = nextIndex;
|
||||
else next[prevIndex] = nextIndex;
|
||||
prev[nextIndex] = prevIndex;
|
||||
linkTail(index);
|
||||
};
|
||||
const _shrink = (newMax) => {
|
||||
let current = tail;
|
||||
const preserve = Math.min(size, newMax);
|
||||
const remove = size - preserve;
|
||||
const newKeyList = new Array(preserve);
|
||||
const newValList = new Array(preserve);
|
||||
for (let i = 0; i < remove; i++) {
|
||||
const key = keyList[head];
|
||||
onEviction == null ? void 0 : onEviction(key, valList[head]);
|
||||
keyMap.delete(key);
|
||||
head = next[head];
|
||||
}
|
||||
for (let i = preserve - 1; i >= 0; i--) {
|
||||
newKeyList[i] = keyList[current];
|
||||
newValList[i] = valList[current];
|
||||
keyMap.set(keyList[current], i);
|
||||
current = prev[current];
|
||||
}
|
||||
head = 0;
|
||||
tail = preserve - 1;
|
||||
size = preserve;
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
for (let i = 0; i < preserve; i++) {
|
||||
keyList[i] = newKeyList[i];
|
||||
valList[i] = newValList[i];
|
||||
next[i] = i + 1;
|
||||
prev[i] = i - 1;
|
||||
}
|
||||
free = [];
|
||||
for (let i = preserve; i < newMax; i++) free.push(i);
|
||||
};
|
||||
const _grow = (newMax) => {
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
keyList.fill(void 0, max);
|
||||
valList.fill(void 0, max);
|
||||
next.fill(0, max);
|
||||
prev.fill(0, max);
|
||||
const _evict = () => {
|
||||
const evictHead = head;
|
||||
const key = keyList[evictHead];
|
||||
onEviction == null ? void 0 : onEviction(key, valList[evictHead]);
|
||||
keyMap.delete(key);
|
||||
keyList[evictHead] = void 0;
|
||||
valList[evictHead] = void 0;
|
||||
head = next[evictHead];
|
||||
if (head !== 0) prev[head] = 0;
|
||||
size--;
|
||||
if (size === 0) head = tail = 0;
|
||||
free.push(evictHead);
|
||||
return evictHead;
|
||||
};
|
||||
return {
|
||||
/** Adds a key-value pair to the cache. Updates the value if the key already exists. */
|
||||
@@ -77,33 +44,20 @@ const createLRU = (options) => {
|
||||
if (key === void 0) return;
|
||||
let index = keyMap.get(key);
|
||||
if (index === void 0) {
|
||||
if (size === max) {
|
||||
index = head;
|
||||
const evictKey = keyList[index];
|
||||
onEviction == null ? void 0 : onEviction(evictKey, valList[index]);
|
||||
keyMap.delete(evictKey);
|
||||
head = next[index];
|
||||
prev[head] = 0;
|
||||
} else {
|
||||
index = free.length > 0 ? free.pop() : size;
|
||||
size++;
|
||||
}
|
||||
index = size === max ? _evict() : free.length > 0 ? free.pop() : size;
|
||||
keyMap.set(key, index);
|
||||
keyList[index] = key;
|
||||
valList[index] = value;
|
||||
if (size === 1) head = tail = index;
|
||||
else linkTail(index);
|
||||
} else {
|
||||
onEviction == null ? void 0 : onEviction(key, valList[index]);
|
||||
valList[index] = value;
|
||||
moveToTail(index);
|
||||
}
|
||||
size++;
|
||||
} else onEviction == null ? void 0 : onEviction(key, valList[index]);
|
||||
valList[index] = value;
|
||||
if (size === 1) head = tail = index;
|
||||
else setTail(index, "set");
|
||||
},
|
||||
/** Retrieves the value for a given key and moves the key to the most recent position. */
|
||||
get(key) {
|
||||
const index = keyMap.get(key);
|
||||
if (index === void 0) return;
|
||||
if (index !== tail) moveToTail(index);
|
||||
if (index !== tail) setTail(index, "get");
|
||||
return valList[index];
|
||||
},
|
||||
/** Retrieves the value for a given key without changing its position. */
|
||||
@@ -158,10 +112,10 @@ const createLRU = (options) => {
|
||||
valList[index] = void 0;
|
||||
const prevIndex = prev[index];
|
||||
const nextIndex = next[index];
|
||||
if (prevIndex !== 0) next[prevIndex] = nextIndex;
|
||||
if (nextIndex !== 0) prev[nextIndex] = prevIndex;
|
||||
if (index === head) head = nextIndex;
|
||||
else next[prevIndex] = nextIndex;
|
||||
if (index === tail) tail = prevIndex;
|
||||
else prev[nextIndex] = prevIndex;
|
||||
size--;
|
||||
return true;
|
||||
},
|
||||
@@ -169,28 +123,16 @@ const createLRU = (options) => {
|
||||
evict: (number) => {
|
||||
let toPrune = Math.min(number, size);
|
||||
while (toPrune > 0) {
|
||||
const evictHead = head;
|
||||
const key = keyList[evictHead];
|
||||
onEviction == null ? void 0 : onEviction(key, valList[evictHead]);
|
||||
keyMap.delete(key);
|
||||
keyList[evictHead] = void 0;
|
||||
valList[evictHead] = void 0;
|
||||
head = next[evictHead];
|
||||
prev[head] = 0;
|
||||
size--;
|
||||
free.push(evictHead);
|
||||
_evict();
|
||||
toPrune--;
|
||||
}
|
||||
if (size === 0) head = tail = 0;
|
||||
},
|
||||
/** Clears all key-value pairs from the cache. */
|
||||
clear() {
|
||||
if (onEviction) {
|
||||
let current = head;
|
||||
for (let i = 0; i < size; i++) {
|
||||
onEviction(keyList[current], valList[current]);
|
||||
current = next[current];
|
||||
}
|
||||
if (typeof onEviction === "function") {
|
||||
const iterator = keyMap.values();
|
||||
for (let result = iterator.next(); !result.done; result = iterator.next())
|
||||
onEviction(keyList[result.value], valList[result.value]);
|
||||
}
|
||||
keyMap.clear();
|
||||
keyList.fill(void 0);
|
||||
@@ -204,8 +146,46 @@ const createLRU = (options) => {
|
||||
if (!(Number.isInteger(newMax) && newMax > 0))
|
||||
throw new TypeError("`max` must be a positive integer");
|
||||
if (newMax === max) return;
|
||||
if (newMax < max) _shrink(newMax);
|
||||
else _grow(newMax);
|
||||
if (newMax < max) {
|
||||
let current = tail;
|
||||
const preserve = Math.min(size, newMax);
|
||||
const remove = size - preserve;
|
||||
const newKeyList = new Array(newMax);
|
||||
const newValList = new Array(newMax);
|
||||
const newNext = new Array(newMax);
|
||||
const newPrev = new Array(newMax);
|
||||
for (let i = 1; i <= remove; i++)
|
||||
onEviction == null ? void 0 : onEviction(keyList[i], valList[i]);
|
||||
for (let i = preserve - 1; i >= 0; i--) {
|
||||
newKeyList[i] = keyList[current];
|
||||
newValList[i] = valList[current];
|
||||
newNext[i] = i + 1;
|
||||
newPrev[i] = i - 1;
|
||||
keyMap.set(newKeyList[i], i);
|
||||
current = prev[current];
|
||||
}
|
||||
head = 0;
|
||||
tail = preserve - 1;
|
||||
size = preserve;
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
for (let i = 0; i < preserve; i++) {
|
||||
keyList[i] = newKeyList[i];
|
||||
valList[i] = newValList[i];
|
||||
next[i] = newNext[i];
|
||||
prev[i] = newPrev[i];
|
||||
}
|
||||
free = [];
|
||||
for (let i = preserve; i < newMax; i++) free.push(i);
|
||||
} else {
|
||||
const fill = newMax - max;
|
||||
keyList.push(...new Array(fill).fill(void 0));
|
||||
valList.push(...new Array(fill).fill(void 0));
|
||||
next.push(...new Array(fill).fill(0));
|
||||
prev.push(...new Array(fill).fill(0));
|
||||
}
|
||||
max = newMax;
|
||||
},
|
||||
/** Returns the maximum number of items that can be stored in the cache. */
|
||||
|
||||
30
node_modules/lru.min/package.json
generated
vendored
30
node_modules/lru.min/package.json
generated
vendored
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "lru.min",
|
||||
"version": "1.1.4",
|
||||
"description": "🔥 An extremely fast and efficient LRU cache for JavaScript with high compatibility (including Browsers).",
|
||||
"version": "1.1.2",
|
||||
"description": "🔥 An extremely fast and efficient LRU cache for JavaScript with high compatibility (including Browsers) — 6.8KB.",
|
||||
"main": "./lib/index.js",
|
||||
"module": "./lib/index.mjs",
|
||||
"types": "./lib/index.d.ts",
|
||||
@@ -30,32 +30,34 @@
|
||||
"scripts": {
|
||||
"benchmark:esm": "cd benchmark && npm ci && node index.mjs",
|
||||
"benchmark:cjs": "cd benchmark && npm ci && node index.cjs",
|
||||
"prebuild": "rm -rf ./browser ./lib",
|
||||
"build:browser": "tsx tools/browserfy.ts",
|
||||
"build:esm": "esbuild src/index.ts --outfile=lib/index.mjs --platform=node --target=node12 --format=esm",
|
||||
"build": "rm -rf ./browser ./lib && tsc && npm run build:esm && npm run build:browser",
|
||||
"build": "tsc && npm run build:esm && npm run build:browser",
|
||||
"test:node": "poku",
|
||||
"test:bun": "bun poku",
|
||||
"test:deno": "deno run -A npm:poku",
|
||||
"test:coverage": "mcr --import tsx --config mcr.config.ts npm run test:node",
|
||||
"lint": "npx @biomejs/biome lint && prettier --check .",
|
||||
"lint:fix": "npx @biomejs/biome lint --write && prettier --write .github/workflows/*.yml .",
|
||||
"update": "pu minor && npm i && (npm audit fix || true) && npm run lint:fix",
|
||||
"update": "pu minor && npm i && npm audit fix",
|
||||
"postupdate": "npm run lint:fix",
|
||||
"size": "ls -lh lib/index.mjs | awk '{print $5}'"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.29.0",
|
||||
"@babel/preset-env": "^7.29.0",
|
||||
"@babel/core": "^7.26.9",
|
||||
"@babel/preset-env": "^7.26.9",
|
||||
"@biomejs/biome": "^1.9.4",
|
||||
"@types/babel__core": "^7.20.5",
|
||||
"@types/node": "^24.10.9",
|
||||
"esbuild": "^0.27.2",
|
||||
"monocart-coverage-reports": "^2.12.9",
|
||||
"@types/node": "^22.13.10",
|
||||
"esbuild": "^0.25.0",
|
||||
"monocart-coverage-reports": "2.12.1",
|
||||
"packages-update": "^2.0.0",
|
||||
"poku": "^3.0.3-canary.13a996a9",
|
||||
"prettier": "^3.8.1",
|
||||
"terser": "^5.46.0",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "^5.9.3"
|
||||
"poku": "^3.0.1",
|
||||
"prettier": "^3.5.3",
|
||||
"terser": "^5.39.0",
|
||||
"tsx": "^4.19.3",
|
||||
"typescript": "^5.8.2"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
|
||||
Reference in New Issue
Block a user