Skip to content

Commit

Permalink
chore: improve setTail
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Aug 27, 2024
1 parent 789695f commit d4d3429
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![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.6KB**.
🔥 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>

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lru.min",
"version": "0.3.1",
"description": "🔥 An extremely fast and efficient LRU cache for JavaScript (Browser compatible) — 6.6KB.",
"description": "🔥 An extremely fast and efficient LRU cache for JavaScript (Browser compatible) — 6.7KB.",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
Expand All @@ -28,7 +28,7 @@
"deno": ">=1.30.0"
},
"scripts": {
"benchmark": "cd benchmark && npm ci && node lib/index.js",
"benchmark": "cd benchmark && npm ci && node index.mjs",
"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",
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export const createLRU = <Key, Value>(options: {
const next: number[] = new Array(max).fill(0);
const prev: number[] = new Array(max).fill(0);

const moveToTail = (index: number): undefined => {
const setTail = (index: number, type: 'set' | 'get'): undefined => {
if (index === tail) return;

const nextIndex = next[index];
const prevIndex = prev[index];

if (index === head) head = nextIndex;
else if (prevIndex !== 0) next[prevIndex] = nextIndex;
else if (type === 'get' || prevIndex !== 0) next[prevIndex] = nextIndex;

if (nextIndex !== 0) prev[nextIndex] = prevIndex;

Expand Down Expand Up @@ -74,7 +74,7 @@ export const createLRU = <Key, Value>(options: {
valList[index] = value;

if (size === 1) head = tail = index;
else moveToTail(index);
else setTail(index, 'set');
},

/** Retrieves the value for a given key and moves the key to the most recent position. */
Expand All @@ -83,7 +83,8 @@ export const createLRU = <Key, Value>(options: {

if (index === undefined) return;

moveToTail(index);
if (index !== tail) setTail(index, 'get');

return valList[index];
},

Expand Down
13 changes: 13 additions & 0 deletions test/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ describe('Order Suite', () => {
['key1', 'value1'],
]
);

assert.strictEqual(LRU.get('key2'), 'value2');
assert.strictEqual(LRU.get('key1'), 'value1');

assert.deepStrictEqual(
[...LRU.entries()],
[
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3'],
['key4', 'value4'],
]
);
});

it('should not change the order when an item is peeked', () => {
Expand Down

0 comments on commit d4d3429

Please sign in to comment.