Skip to content

Commit

Permalink
Verbose statements, fixing remove() by moving a notify() into a c…
Browse files Browse the repository at this point in the history
…onditional statement when a key is valid, adding `benchmark.js` for dev purposes
  • Loading branch information
avoidwork committed Nov 24, 2018
1 parent f64d28f commit c02f7a8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 35 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
.eslintrc
.travis.yml
Gruntfile.js
benchmark.js
39 changes: 39 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const path = require("path"),
lru = require(path.join(__dirname, "lib", "tiny-lru.js")),
precise = require("precise"),
nth = 2e5,
cache = lru(nth),
data = new Array(nth);

function seed () {
let i = -1;

while (++i < nth) {
data[i] = Math.floor(Math.random() * nth);
}
}

function populate (arg, start = 0) {
const nth = arg.max;
let i = -1;

while (++i < nth) {
arg.set(i + start, data[i]);
}
}

function bench (n = 0, x = 1) {
seed();

const timer = precise().start();

populate(cache, n);
timer.stop();
console.log(`Run ${x} ${x === 1 ? "Set" : "Evict"} (${n === 0 ? "Low Keys" : "High Keys"}): ${timer.diff() / 1e6} ms`);
}

console.log(`Benchmarking ${nth} items (random value per run)`);
bench();
bench(nth, 2);
bench(void 0, 3);
bench(nth, 4);
34 changes: 18 additions & 16 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
clear (silent = false) {
this.reset();

if (!silent && this.notify) {
if (silent === false && this.notify === true) {
next(this.onchange("clear", this.dump()));
}

return this;
}

clearTimer (key, col = "timers") {
if (this.has(key, col)) {
if (this.has(key, col) === true) {
clearTimeout(this[col][key]);
delete this[col][key];
}
Expand Down Expand Up @@ -62,7 +62,7 @@
evict () {
this.remove(this.last, true);

if (this.notify) {
if (this.notify === true) {
next(this.onchange("evict", this.dump()));
}

Expand All @@ -72,15 +72,15 @@
get (key) {
let output;

if (this.has(key)) {
if (this.has(key) === true) {
output = this.cache[key].value;
this.set(key, output);

if (this.ttl > 0) {
this.clearTimer(key).setTimer(key);
}

if (this.notify) {
if (this.notify === true) {
next(this.onchange("get", this.dump()));
}
}
Expand All @@ -97,7 +97,7 @@
remove (key, silent = false) {
let result;

if (this.has(key)) {
if (this.has(key) === true) {
const cached = this.cache[key];

delete this.cache[key];
Expand All @@ -111,7 +111,7 @@
this.clearTimer(key, "expires");
}

if (this.has(cached.previous)) {
if (this.has(cached.previous) === true) {
this.cache[cached.previous].next = cached.next;

if (this.first === key) {
Expand All @@ -121,7 +121,7 @@
this.first = empty;
}

if (this.has(cached.next)) {
if (this.has(cached.next) === true) {
this.cache[cached.next].previous = cached.previous;

if (this.last === key) {
Expand All @@ -132,10 +132,10 @@
}

result = cached;
}

if (!silent && this.notify) {
next(this.onchange("remove", this.dump()));
if (silent === false && this.notify === true) {
next(this.onchange("remove", this.dump()));
}
}

return result;
Expand Down Expand Up @@ -163,7 +163,7 @@
set (key, value) {
let first, item;

if (this.has(key)) {
if (this.has(key) === true) {
item = this.cache[key];
item.value = value;
item.next = empty;
Expand All @@ -176,10 +176,12 @@
this.last = item.previous;
}
} else {
if (++this.length > this.max) {
this.remove(this.last, true);
if (this.length === this.max) {
this.evict();
}

this.length++;

if (this.length === 1) {
this.last = key;
}
Expand All @@ -191,7 +193,7 @@
};
}

if (this.first !== key && this.has(this.first)) {
if (this.first !== key && this.has(this.first) === true) {
first = this.cache[this.first];
first.next = key;

Expand All @@ -202,7 +204,7 @@

this.first = key;

if (this.notify) {
if (this.notify === true) {
next(this.onchange("set", this.dump()));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-lru.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/tiny-lru.min.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"engineStrict": true,
"scripts": {
"benchmark": "node benchmark.js",
"test": "grunt test"
},
"dependencies": {},
Expand All @@ -29,7 +30,8 @@
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-nodeunit": "^2.0.0",
"grunt-contrib-watch": "^1.1.0",
"grunt-eslint": "^21.0.0"
"grunt-eslint": "^21.0.0",
"precise": "^1.1.0"
},
"keywords": [
"LRU",
Expand Down
34 changes: 18 additions & 16 deletions src/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
clear (silent = false) {
this.reset();

if (!silent && this.notify) {
if (silent === false && this.notify === true) {
next(this.onchange("clear", this.dump()));
}

return this;
}

clearTimer (key, col = "timers") {
if (this.has(key, col)) {
if (this.has(key, col) === true) {
clearTimeout(this[col][key]);
delete this[col][key];
}
Expand Down Expand Up @@ -47,7 +47,7 @@
evict () {
this.remove(this.last, true);

if (this.notify) {
if (this.notify === true) {
next(this.onchange("evict", this.dump()));
}

Expand All @@ -57,15 +57,15 @@
get (key) {
let output;

if (this.has(key)) {
if (this.has(key) === true) {
output = this.cache[key].value;
this.set(key, output);

if (this.ttl > 0) {
this.clearTimer(key).setTimer(key);
}

if (this.notify) {
if (this.notify === true) {
next(this.onchange("get", this.dump()));
}
}
Expand All @@ -82,7 +82,7 @@
remove (key, silent = false) {
let result;

if (this.has(key)) {
if (this.has(key) === true) {
const cached = this.cache[key];

delete this.cache[key];
Expand All @@ -96,7 +96,7 @@
this.clearTimer(key, "expires");
}

if (this.has(cached.previous)) {
if (this.has(cached.previous) === true) {
this.cache[cached.previous].next = cached.next;

if (this.first === key) {
Expand All @@ -106,7 +106,7 @@
this.first = empty;
}

if (this.has(cached.next)) {
if (this.has(cached.next) === true) {
this.cache[cached.next].previous = cached.previous;

if (this.last === key) {
Expand All @@ -117,10 +117,10 @@
}

result = cached;
}

if (!silent && this.notify) {
next(this.onchange("remove", this.dump()));
if (silent === false && this.notify === true) {
next(this.onchange("remove", this.dump()));
}
}

return result;
Expand Down Expand Up @@ -148,7 +148,7 @@
set (key, value) {
let first, item;

if (this.has(key)) {
if (this.has(key) === true) {
item = this.cache[key];
item.value = value;
item.next = empty;
Expand All @@ -161,10 +161,12 @@
this.last = item.previous;
}
} else {
if (++this.length > this.max) {
this.remove(this.last, true);
if (this.length === this.max) {
this.evict();
}

this.length++;

if (this.length === 1) {
this.last = key;
}
Expand All @@ -176,7 +178,7 @@
};
}

if (this.first !== key && this.has(this.first)) {
if (this.first !== key && this.has(this.first) === true) {
first = this.cache[this.first];
first.next = key;

Expand All @@ -187,7 +189,7 @@

this.first = key;

if (this.notify) {
if (this.notify === true) {
next(this.onchange("set", this.dump()));
}

Expand Down

0 comments on commit c02f7a8

Please sign in to comment.