Comparing version 0.3.1 to 0.3.2
@@ -1,1 +0,1 @@ | ||
var createLRU=k=>{var{max:t,onEviction:d}=k;if(!(Number.isInteger(t)&&t>0))throw new TypeError("`max` must be a positive integer");var n=0,m=0,i=0,b=[],y=new Map,l=new Array(t).fill(void 0),f=new Array(t).fill(void 0),s=new Array(t).fill(0),v=new Array(t).fill(0),p=e=>{if(e!==i){var r=s[e],u=v[e];e===m?m=r:u!==0&&(s[u]=r),r!==0&&(v[r]=u),s[i]=e,v[e]=i,s[e]=0,i=e}},A=()=>{var e=m,r=l[e];return d==null||d(r,f[e]),y.delete(r),l[e]=void 0,f[e]=void 0,m=s[e],m!==0&&(v[m]=0),n--,n===0&&(m=i=0),e};return{set(e,r){var u=y.get(e);u===void 0?(u=n===t?A():b.length>0?b.pop():n,y.set(e,u),l[u]=e,n++):d==null||d(e,f[u]),f[u]=r,n===1?m=i=u:p(u)},get(e){var r=y.get(e);if(r!==void 0)return p(r),f[r]},peek:e=>{var r=y.get(e);return r!==void 0?f[r]:void 0},has:e=>y.has(e),*keys(){for(var e=i,r=0;r<n;r++)yield l[e],e=v[e]},*values(){for(var e=i,r=0;r<n;r++)yield f[e],e=v[e]},*entries(){for(var e=i,r=0;r<n;r++)yield[l[e],f[e]],e=v[e]},forEach:e=>{for(var r=i,u=0;u<n;u++){var o=l[r],h=f[r];e(h,o),r=v[r]}},delete(e){var r=y.get(e);return r!==void 0?(d==null||d(e,f[r]),y.delete(e),b.push(r),l[r]=void 0,f[r]=void 0,n--,!0):!1},evict:e=>{for(var r=Math.min(e,n);r>0;)A(),r--},clear(){for(var e of y.values())d==null||d(l[e],f[e]);y.clear(),l.fill(void 0),f.fill(void 0),b=[],n=0,m=i=0},resize:e=>{if(!(Number.isInteger(e)&&e>0))throw new TypeError("`max` must be a positive integer");if(e!==t){if(e<t){for(var r=i,u=Math.min(n,e),o=n-u,h=new Array(e),K=new Array(e),I=new Array(e),V=new Array(e),a=1;a<=o;a++)d==null||d(l[a],f[a]);for(var a=u-1;a>=0;a--)h[a]=l[r],K[a]=f[r],I[a]=a+1,V[a]=a-1,y.set(h[a],a),r=v[r];m=0,i=u-1,n=u,l.length=e,f.length=e,s.length=e,v.length=e;for(var a=0;a<u;a++)l[a]=h[a],f[a]=K[a],s[a]=I[a],v[a]=V[a];b=[];for(var a=u;a<e;a++)b.push(a)}else{var g=e-t;l.push(...new Array(g).fill(void 0)),f.push(...new Array(g).fill(void 0)),s.push(...new Array(g).fill(0)),v.push(...new Array(g).fill(0))}t=e}},get max(){return t},get size(){return n},get available(){return t-n}}}; | ||
var createLRU=k=>{var{max:v,onEviction:t}=k;if(!(Number.isInteger(v)&&v>0))throw new TypeError("`max` must be a positive integer");var u=0,y=0,d=0,h=[],m=new Map,l=new Array(v).fill(void 0),f=new Array(v).fill(void 0),b=new Array(v).fill(0),i=new Array(v).fill(0),o=(e,r)=>{if(e!==d){var n=b[e],s=i[e];e===y?y=n:(r==="get"||s!==0)&&(b[s]=n),n!==0&&(i[n]=s),b[d]=e,i[e]=d,b[e]=0,d=e}},A=()=>{var e=y,r=l[e];return t==null||t(r,f[e]),m.delete(r),l[e]=void 0,f[e]=void 0,y=b[e],y!==0&&(i[y]=0),u--,u===0&&(y=d=0),h.push(e),e};return{set(e,r){if(e!==void 0){var n=m.get(e);n===void 0?(n=u===v?A():h.length>0?h.pop():u,m.set(e,n),l[n]=e,u++):t==null||t(e,f[n]),f[n]=r,u===1?y=d=n:o(n,"set")}},get(e){var r=m.get(e);if(r!==void 0)return r!==d&&o(r,"get"),f[r]},peek:e=>{var r=m.get(e);return r!==void 0?f[r]:void 0},has:e=>m.has(e),*keys(){for(var e=d,r=0;r<u;r++)yield l[e],e=i[e]},*values(){for(var e=d,r=0;r<u;r++)yield f[e],e=i[e]},*entries(){for(var e=d,r=0;r<u;r++)yield[l[e],f[e]],e=i[e]},forEach:e=>{for(var r=d,n=0;n<u;n++){var s=l[r],g=f[r];e(g,s),r=i[r]}},delete(e){var r=m.get(e);if(r===void 0)return!1;t==null||t(e,f[r]),m.delete(e),h.push(r),l[r]=void 0,f[r]=void 0;var n=i[r],s=b[r];return n!==0&&(b[n]=s),s!==0&&(i[s]=n),r===y&&(y=s),r===d&&(d=n),u--,!0},evict:e=>{for(var r=Math.min(e,u);r>0;)A(),r--},clear(){for(var e of m.values())t==null||t(l[e],f[e]);m.clear(),l.fill(void 0),f.fill(void 0),h=[],u=0,y=d=0},resize:e=>{if(!(Number.isInteger(e)&&e>0))throw new TypeError("`max` must be a positive integer");if(e!==v){if(e<v){for(var r=d,n=Math.min(u,e),s=u-n,g=new Array(e),I=new Array(e),K=new Array(e),V=new Array(e),a=1;a<=s;a++)t==null||t(l[a],f[a]);for(var a=n-1;a>=0;a--)g[a]=l[r],I[a]=f[r],K[a]=a+1,V[a]=a-1,m.set(g[a],a),r=i[r];y=0,d=n-1,u=n,l.length=e,f.length=e,b.length=e,i.length=e;for(var a=0;a<n;a++)l[a]=g[a],f[a]=I[a],b[a]=K[a],i[a]=V[a];h=[];for(var a=n;a<e;a++)h.push(a)}else{var p=e-v;l.push(...new Array(p).fill(void 0)),f.push(...new Array(p).fill(void 0)),b.push(...new Array(p).fill(0)),i.push(...new Array(p).fill(0))}v=e}},get max(){return v},get size(){return u},get available(){return v-u}}}; |
@@ -17,3 +17,3 @@ "use strict"; | ||
const prev = new Array(max).fill(0); | ||
const moveToTail = (index) => { | ||
const setTail = (index, type) => { | ||
if (index === tail) | ||
@@ -25,3 +25,3 @@ return; | ||
head = nextIndex; | ||
else if (prevIndex !== 0) | ||
else if (type === 'get' || prevIndex !== 0) | ||
next[prevIndex] = nextIndex; | ||
@@ -48,2 +48,3 @@ if (nextIndex !== 0) | ||
head = tail = 0; | ||
free.push(evictHead); | ||
return evictHead; | ||
@@ -54,2 +55,4 @@ }; | ||
set(key, value) { | ||
if (key === undefined) | ||
return; | ||
let index = keyMap.get(key); | ||
@@ -68,3 +71,3 @@ if (index === undefined) { | ||
else | ||
moveToTail(index); | ||
setTail(index, 'set'); | ||
}, | ||
@@ -76,3 +79,4 @@ /** Retrieves the value for a given key and moves the key to the most recent position. */ | ||
return; | ||
moveToTail(index); | ||
if (index !== tail) | ||
setTail(index, 'get'); | ||
return valList[index]; | ||
@@ -124,12 +128,21 @@ }, | ||
const index = keyMap.get(key); | ||
if (index !== undefined) { | ||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(key, valList[index]); | ||
keyMap.delete(key); | ||
free.push(index); | ||
keyList[index] = undefined; | ||
valList[index] = undefined; | ||
size--; | ||
return true; | ||
} | ||
return false; | ||
if (index === undefined) | ||
return false; | ||
onEviction === null || onEviction === void 0 ? void 0 : onEviction(key, valList[index]); | ||
keyMap.delete(key); | ||
free.push(index); | ||
keyList[index] = undefined; | ||
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; | ||
if (index === tail) | ||
tail = prevIndex; | ||
size--; | ||
return true; | ||
}, | ||
@@ -136,0 +149,0 @@ /** Evicts the oldest item or the specified number of the oldest items from the cache. */ |
{ | ||
"name": "lru.min", | ||
"version": "0.3.1", | ||
"description": "π₯ An extremely fast and efficient LRU cache for JavaScript (Browser compatible) β 6.4KB.", | ||
"version": "0.3.2", | ||
"description": "π₯ An extremely fast and efficient LRU cache for JavaScript (Browser compatible) β 6.7KB.", | ||
"main": "./lib/index.js", | ||
@@ -31,3 +31,4 @@ "module": "./lib/index.mjs", | ||
"scripts": { | ||
"benchmark": "cd benchmark && npm ci && node lib/index.js", | ||
"benchmark:esm": "cd benchmark && npm ci && node index.mjs", | ||
"benchmark:cjs": "cd benchmark && npm ci && node index.cjs", | ||
"prebuild": "rm -rf ./browser ./lib", | ||
@@ -56,3 +57,3 @@ "build:browser": "tsx tools/browserfy.ts", | ||
"prettier": "^3.3.3", | ||
"tsx": "^4.17.0", | ||
"tsx": "^4.19.0", | ||
"typescript": "^5.5.4" | ||
@@ -59,0 +60,0 @@ }, |
<h1 align="center">lru.min</h1> | ||
<div align="center"> | ||
π₯ An extremely fast and efficient <strong><a href="https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29">LRU</a> Cache</strong> for <strong>JavaScript</strong> (<strong>Browser</strong> compatible) β **6.4KB**. | ||
[![NPM Version](https://img.shields.io/npm/v/lru.min.svg?label=&color=70a1ff&logo=npm&logoColor=white)](https://www.npmjs.com/package/lru.min) | ||
[![Coverage](https://img.shields.io/codecov/c/github/wellwelwel/lru.min?label=&logo=codecov&logoColor=white&color=98cc00)](https://github.com/wellwelwel/lru.min/tree/main/.nycrc)<br /> | ||
[![GitHub Workflow Status (Node.js)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_node.yml?event=push&label=&branch=main&logo=nodedotjs&logoColor=535c68&color=badc58)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_node.yml?query=branch%3Amain) | ||
[![GitHub Workflow Status (Bun)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_bun.yml?event=push&label=&branch=main&logo=bun&logoColor=ffffff&color=f368e0)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_bun.yml?query=branch%3Amain) | ||
[![GitHub Workflow Status (Deno)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_deno.yml?event=push&label=&branch=main&logo=deno&logoColor=ffffff&color=079992)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_deno.yml?query=branch%3Amain) | ||
π₯ An extremely fast and efficient <strong><a href="https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29">LRU</a> Cache</strong> for <strong>JavaScript</strong> (<strong>Browser</strong> compatible) β **6.7KB**. | ||
</div> | ||
@@ -35,5 +41,2 @@ | ||
> - β οΈ Please wait until `v1.x.x` before using this package. | ||
> - π Public repository coming soon. | ||
### Import | ||
@@ -88,2 +91,4 @@ | ||
> `undefined` keys will simply be ignored. | ||
### Get a cache | ||
@@ -209,48 +214,19 @@ | ||
> You can see how the tests are run and compared in the [benchmark](https://github.com/wellwelwel/lru.min/tree/main/benchmark) directory. | ||
> | ||
> - [**lru-cache**](https://github.com/isaacs/node-lru-cache) `v11.0.0` | ||
> - [**quick-lru**](https://github.com/sindresorhus/quick-lru) `v7.0.0` | ||
#### Node.js | ||
```sh | ||
# ES Modules | ||
lru.min: 247.45ms | ||
lru-cache: 255.93ms | ||
quick-lru: 312.90ms | ||
``` | ||
# Time: | ||
lru.min: 240.45ms | ||
lru-cache: 258.32ms | ||
quick-lru: 279.89ms | ||
```sh | ||
# CommonJS | ||
lru.min: 236.35ms | ||
lru-cache: 258.74ms | ||
quick-lru: not compatible | ||
# CPU: | ||
lru.min: 275558.30Β΅s | ||
lru-cache: 306858.30Β΅s | ||
quick-lru: 401318.80Β΅s | ||
``` | ||
#### Bun | ||
- See detailed results and how the tests are run and compared in the [benchmark](https://github.com/wellwelwel/lru.min/tree/main/benchmark) directory. | ||
```sh | ||
# ES Modules | ||
lru.min: 298.42ms | ||
quick-lru: 315.37ms | ||
lru-cache: 359.23ms | ||
``` | ||
```sh | ||
# CommonJS | ||
lru.min: 363.79ms | ||
lru-cache: 371.39ms | ||
quick-lru: not compatible | ||
``` | ||
#### Deno | ||
```sh | ||
# ES Modules | ||
lru.min: 222.60ms | ||
lru-cache: 227.80ms | ||
quick-lru: 253.00ms | ||
``` | ||
--- | ||
@@ -260,2 +236,4 @@ | ||
[![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_codeql.yml?event=push&label=&branch=main&logo=github&logoColor=white&color=f368e0)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_codeql.yml?query=branch%3Amain) | ||
Please check the [**SECURITY.md**](https://github.com/wellwelwel/lru.min/blob/main/SECURITY.md). | ||
@@ -273,4 +251,4 @@ | ||
> **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. | ||
> | ||
**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. | ||
> For more comprehensive features such as **TTL** support, consider using and supporting them π€ | ||
@@ -277,0 +255,0 @@ |
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
30054
474
261