Skip to content

Commit

Permalink
Revert "Use Map for faster execution"
Browse files Browse the repository at this point in the history
This reverts commit 6ba3f32.
  • Loading branch information
avoidwork committed Apr 2, 2023
1 parent 4351fc1 commit cc91ecc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
59 changes: 31 additions & 28 deletions src/lru.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
class LRU {
get size () {
return this.items.size;
}

constructor (max = 0, ttl = 0, resetTtl = false) {
this.first = null;
this.items = new Map();
this.items = Object.create(null);
this.last = null;
this.max = max;
this.resetTtl = resetTtl;
this.size = 0;
this.ttl = ttl;
}

#has (key) {
return key in this.items;
}

clear () {
this.first = null;
this.items = new Map();
this.items = Object.create(null);
this.last = null;
this.size = 0;

return this;
}

delete (key) {
if (this.items.has(key)) {
const item = this.items.get(key);
if (this.#has(key)) {
const item = this.items[key];

this.items.delete(key);
delete this.items[key];
this.size--;

if (item.prev !== null) {
item.prev.next = item.next;
Expand All @@ -50,7 +53,8 @@ class LRU {
if (bypass || this.size > 0) {
const item = this.first;

this.items.delete(item.key);
delete this.items[item.key];
this.size--;

if (this.size === 0) {
this.first = null;
Expand All @@ -64,21 +68,11 @@ class LRU {
return this;
}

expiresAt (key) {
let result;

if (this.items.has(key)) {
result = this.items.get(key).expiry;
}

return result;
}

get (key) {
let result;

if (this.items.has(key)) {
const item = this.items.get(key);
if (this.#has(key)) {
const item = this.items[key];

if (this.ttl > 0 && item.expiry <= Date.now()) {
this.delete(key);
Expand All @@ -91,15 +85,25 @@ class LRU {
return result;
}

expiresAt (key) {
let result;

if (this.#has(key)) {
result = this.items[key].expiry;
}

return result;
}

keys () {
return this.items.keys();
return Object.keys(this.items);
}

set (key, value, bypass = false, resetTtl = this.resetTtl) {
let item;

if (bypass || this.items.has(key)) {
item = this.items.get(key);
if (bypass || this.#has(key)) {
item = this.items[key];
item.value = value;

if (resetTtl) {
Expand Down Expand Up @@ -132,16 +136,15 @@ class LRU {
this.evict(true);
}

item = {
item = this.items[key] = {
expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,
key: key,
prev: this.last,
next: null,
value
};
this.items.set(key, item);

if (this.size === 1) {
if (++this.size === 1) {
this.first = item;
} else {
this.last.next = item;
Expand Down
56 changes: 28 additions & 28 deletions test/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ describe("Testing functionality", function () {
assert.strictEqual(this.cache.first.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.last.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.size, 4, "Should be '4'");
assert.strictEqual(this.cache.items.get("e").next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.get("e").prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("d").next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.get("d").prev.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.get("c").next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("c").prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.get("b").next.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.get("b").prev, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.d.next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.d.prev.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.c.next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.c.prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.b.next.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.b.prev, null, "Should be 'null'");
this.cache.delete("c");
assert.strictEqual(this.cache.first.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.last.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.size, 3, "Should be '3'");
assert.strictEqual(this.cache.items.get("e").next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.get("e").prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("d").next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.get("d").prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.get("b").next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("b").prev, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.d.next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.d.prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.b.next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.b.prev, null, "Should be 'null'");
this.cache.delete("e");
assert.strictEqual(this.cache.first.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.last.key, "d", "Should be 'd'");
Expand All @@ -58,24 +58,24 @@ describe("Testing functionality", function () {
assert.strictEqual(this.cache.first.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.last.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.size, 4, "Should be '4'");
assert.strictEqual(this.cache.items.get("e").next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.get("e").prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("d").next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.get("d").prev.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.get("c").next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("c").prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.get("b").next.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.get("b").prev, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.d.next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.d.prev.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.c.next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.c.prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.b.next.key, "c", "Should be 'c'");
assert.strictEqual(this.cache.items.b.prev, null, "Should be 'null'");
this.cache.delete("c");
assert.strictEqual(this.cache.first.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.last.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.size, 3, "Should be '3'");
assert.strictEqual(this.cache.items.get("e").next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.get("e").prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("d").next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.get("d").prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.get("b").next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.get("b").prev, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.next, null, "Should be 'null'");
assert.strictEqual(this.cache.items.e.prev.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.d.next.key, "e", "Should be 'e'");
assert.strictEqual(this.cache.items.d.prev.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.items.b.next.key, "d", "Should be 'd'");
assert.strictEqual(this.cache.items.b.prev, null, "Should be 'null'");
this.cache.delete("e");
assert.strictEqual(this.cache.first.key, "b", "Should be 'b'");
assert.strictEqual(this.cache.last.key, "d", "Should be 'd'");
Expand Down

0 comments on commit cc91ecc

Please sign in to comment.