Skip to content

Commit

Permalink
Refactoring set() to avoid ops & fix an assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Dec 7, 2018
1 parent 42536c7 commit 8b6719a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
29 changes: 15 additions & 14 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2018
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @version 4.0.4
* @version 4.0.5
*/
"use strict";

Expand Down Expand Up @@ -136,28 +136,29 @@

set (key, value, silent = false, bypass = false) {
if (bypass === true || this.has(key) === true) {
const item = this.cache[key],
left = item.next,
right = item.previous;
const item = this.cache[key];

item.value = value;
item.next = empty;

if (this.first !== key) {
const left = item.next,
right = item.previous;

item.next = empty;
item.previous = this.first;
link(this.cache[this.first], key, "next", "previous");
}

if (right !== empty && left !== empty && right !== item.previous) {
this.cache[right].next = left;
}
if (left !== empty && left !== this.first) {
if (right !== empty) {
this.cache[right].next = left;
}

if (left !== empty) {
this.cache[left].previous = right;
}
this.cache[left].previous = right;
}

if (this.last === key && item.previous !== empty) {
this.last = item.previous;
if (this.last === key) {
this.last = item.previous;
}
}
} else {
if (this.length === this.max) {
Expand Down
4 changes: 2 additions & 2 deletions lib/tiny-lru.min.js

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

0 comments on commit 8b6719a

Please sign in to comment.