Skip to content

Commit 8999e83

Browse files
committed
Performance improvement
1 parent 76688c6 commit 8999e83

File tree

6 files changed

+36
-39
lines changed

6 files changed

+36
-39
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ const cache = new Cache();
4141
cache.set("a", 10);
4242

4343
// Check data exists in cache
44-
cache.has("a");
44+
cache.has("a"); // true
4545

4646
// Get data from cache
47-
console.log(cache.get("a"));
47+
console.log(cache.get("a")); // 10
4848

4949
// Get all data from cache
5050
cache.forEach(function (data) {
5151
console.log(data); // { a: 10 }
5252
});
5353

5454
// Get all data to array
55-
console.log(cache.toArray());
55+
console.log(cache.toArray()); // [ { a: 10 } ]
5656

5757
// Delete data from cache
5858
cache.delete("a");
@@ -61,24 +61,27 @@ cache.delete("a");
6161
cache.clear();
6262
```
6363

64-
## Create a new cache object
64+
## Create a new cache
6565

6666
To create a new cache we need to create a new instance of lrujs. While creating a new cache we can set the configuration like cache max length and ttl, but it is not mandatory and if we not set any configuration then the default values are used.
6767

6868
Defination:
69+
6970
```js
7071
const cache = new Cache(options);
7172
```
7273

7374
Where options are the following:
75+
7476
- `maxLength` : max length is a cache max length, max length is a positive integer value. The default value is 0, if the value is 0 then it will not check the max length.
7577
- `ttl` : is cache expires time in milliseconds, the default value is 0 and if value if 0 it will not check the ttl.
76-
- `interval` : interval is the time interval in milliseconds, after every interval all the expired values are automatically removed. Default value is 0 and if value is 0 the it will not removes expired values automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
78+
- `interval` : interval is the time interval in milliseconds, after every interval all the expired items are automatically removed. Default value is 0 and if value is 0 the it will not removes expired items automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
7779
- `enableInterval` : enableInterval is a boolean value that is used to enable and disable the interval, the default value is false and if value is explicitly set false then it will not run the interval even if the interval time is set.
7880

79-
Lrujs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When interval is set expired values are removed from cache periodically.
81+
Lrujs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When ttl interval is set, expired items are removed from cache periodically.
8082

8183
Example:
84+
8285
```js
8386
const Cache = require("@opensnip/lrujs");
8487

@@ -94,7 +97,7 @@ const cache = new Cache({
9497

9598
In lrujs any value (both objects and primitive values) may be used as either a key or a value, duplicate keys not allowed and if duplicate item is inserted it will be replaced by the new item.
9699

97-
```js
100+
````js
98101
// Add new data in cache
99102
cache.set("a", 10);
100103

@@ -111,7 +114,7 @@ By default the configuration TTL value is used for every item, but we can set TT
111114
```js
112115
// Add new data in cache
113116
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
114-
```
117+
````
115118
116119
## Get data from cache
117120

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opensnip/lrujs",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Fast and lightweight lru cache for javascript",
55
"main": "index.mjs",
66
"type": "module",

src/cache.cjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = class Cache {
103103
// Insert a new node at head
104104
const existingNode = this.#cache.get(key);
105105
// Update node data if node is already exists
106-
if (existingNode instanceof Node) {
106+
if (typeof existingNode !== "undefined") {
107107
existingNode.value = nodeValue;
108108
// Move current node to the head
109109
this.#linkedList.setHead(existingNode);
@@ -126,7 +126,7 @@ module.exports = class Cache {
126126

127127
const node = this.#cache.get(key);
128128

129-
if (node instanceof Node) {
129+
if (typeof node !== "undefined") {
130130
// Check node is live or not
131131
if (this.#isStale(node)) {
132132
this.delete(key);
@@ -156,15 +156,15 @@ module.exports = class Cache {
156156
delete(key) {
157157
const node = this.#cache.get(key);
158158

159-
if (node instanceof Node) {
159+
if (typeof node !== "undefined") {
160160
this.#linkedList.delete(node);
161161
// Delete node
162162
this.#cache.delete(key);
163163
}
164164
}
165165

166166
#evict() {
167-
if (this.#linkedList.tail == null) return;
167+
if (this.#linkedList.tail === null) return;
168168
if (this.length !== this.#config.maxLength) return;
169169
this.delete(this.#linkedList.tail.value.key);
170170
}
@@ -183,7 +183,7 @@ module.exports = class Cache {
183183

184184
this.#config.intervalId = setInterval(
185185
function (cache) {
186-
if (cache.length == 0) return;
186+
if (cache.length === 0) return;
187187
cache.forEach(function (data) {
188188
// Automatically invalidate expired cache
189189
});
@@ -202,7 +202,7 @@ module.exports = class Cache {
202202
has(key) {
203203
const node = this.#cache.get(key);
204204

205-
if (node instanceof Node) {
205+
if (typeof node !== "undefined") {
206206
// Check node is live or not
207207
if (this.#isStale(node)) {
208208
this.delete(key);

src/cache.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default class Cache {
103103
// Insert a new node at head
104104
const existingNode = this.#cache.get(key);
105105
// Update node data if node is already exists
106-
if (existingNode instanceof Node) {
106+
if (typeof existingNode !== undefined) {
107107
existingNode.value = nodeValue;
108108
// Move current node to the head
109109
this.#linkedList.setHead(existingNode);
@@ -126,7 +126,7 @@ export default class Cache {
126126

127127
const node = this.#cache.get(key);
128128

129-
if (node instanceof Node) {
129+
if (typeof node !== undefined) {
130130
// Check node is live or not
131131
if (this.#isStale(node)) {
132132
this.delete(key);
@@ -156,15 +156,15 @@ export default class Cache {
156156
delete(key) {
157157
const node = this.#cache.get(key);
158158

159-
if (node instanceof Node) {
159+
if (typeof node !== undefined) {
160160
this.#linkedList.delete(node);
161161
// Delete node
162162
this.#cache.delete(key);
163163
}
164164
}
165165

166166
#evict() {
167-
if (this.#linkedList.tail == null) return;
167+
if (this.#linkedList.tail === null) return;
168168
if (this.length !== this.#config.maxLength) return;
169169
this.delete(this.#linkedList.tail.value.key);
170170
}
@@ -183,7 +183,7 @@ export default class Cache {
183183

184184
this.#config.intervalId = setInterval(
185185
function (cache) {
186-
if (cache.length == 0) return;
186+
if (cache.length === 0) return;
187187
cache.forEach(function (data) {
188188
// Automatically invalidate expired cache
189189
});
@@ -202,7 +202,7 @@ export default class Cache {
202202
has(key) {
203203
const node = this.#cache.get(key);
204204

205-
if (node instanceof Node) {
205+
if (typeof node !== undefined) {
206206
// Check node is live or not
207207
if (this.#isStale(node)) {
208208
this.delete(key);

src/linkedlist/index.cjs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ module.exports = class LinkedList {
124124
}
125125
tmpRight.prev = leftNode.prev;
126126
tmpRight.next = leftNode.next;
127-
if (leftNode == this.#head) this.#head = tmpRight;
128-
if (leftNode == this.#tail) this.#tail = tmpRight;
127+
if (leftNode === this.#head) this.#head = tmpRight;
128+
if (leftNode === this.#tail) this.#tail = tmpRight;
129129

130130
// Replace right node with left node
131131
let tmpLeft = new Node(leftNode.value);
@@ -137,8 +137,8 @@ module.exports = class LinkedList {
137137
}
138138
tmpLeft.prev = rightNode.prev;
139139
tmpLeft.next = rightNode.next;
140-
if (rightNode == this.#head) this.#head = tmpLeft;
141-
if (rightNode == this.#tail) this.#tail = tmpLeft;
140+
if (rightNode === this.#head) this.#head = tmpLeft;
141+
if (rightNode === this.#tail) this.#tail = tmpLeft;
142142

143143
delete leftNode.next;
144144
delete leftNode.prev;
@@ -150,7 +150,7 @@ module.exports = class LinkedList {
150150
}
151151

152152
deleteHead() {
153-
if (this.#head == null) return;
153+
if (this.#head === null) return;
154154
if (this.#head.next != null) {
155155
this.#head.next.prev = null;
156156
}
@@ -160,7 +160,7 @@ module.exports = class LinkedList {
160160
}
161161

162162
deleteTail() {
163-
if (this.#tail == null) return;
163+
if (this.#tail === null) return;
164164
if (this.#tail.prev != null) {
165165
this.#tail.prev.next = null;
166166
}
@@ -170,9 +170,6 @@ module.exports = class LinkedList {
170170
}
171171

172172
delete(node) {
173-
if (!(node instanceof Node)) {
174-
throw new TypeError("node should be a valid Node instance");
175-
}
176173
this.detach(node);
177174
delete node.prev;
178175
delete node.next;

src/linkedlist/index.mjs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export default class LinkedList {
124124
}
125125
tmpRight.prev = leftNode.prev;
126126
tmpRight.next = leftNode.next;
127-
if (leftNode == this.#head) this.#head = tmpRight;
128-
if (leftNode == this.#tail) this.#tail = tmpRight;
127+
if (leftNode === this.#head) this.#head = tmpRight;
128+
if (leftNode === this.#tail) this.#tail = tmpRight;
129129

130130
// Replace right node with left node
131131
let tmpLeft = new Node(leftNode.value);
@@ -137,8 +137,8 @@ export default class LinkedList {
137137
}
138138
tmpLeft.prev = rightNode.prev;
139139
tmpLeft.next = rightNode.next;
140-
if (rightNode == this.#head) this.#head = tmpLeft;
141-
if (rightNode == this.#tail) this.#tail = tmpLeft;
140+
if (rightNode === this.#head) this.#head = tmpLeft;
141+
if (rightNode === this.#tail) this.#tail = tmpLeft;
142142

143143
delete leftNode.next;
144144
delete leftNode.prev;
@@ -150,7 +150,7 @@ export default class LinkedList {
150150
}
151151

152152
deleteHead() {
153-
if (this.#head == null) return;
153+
if (this.#head === null) return;
154154
if (this.#head.next != null) {
155155
this.#head.next.prev = null;
156156
}
@@ -160,7 +160,7 @@ export default class LinkedList {
160160
}
161161

162162
deleteTail() {
163-
if (this.#tail == null) return;
163+
if (this.#tail === null) return;
164164
if (this.#tail.prev != null) {
165165
this.#tail.prev.next = null;
166166
}
@@ -170,9 +170,6 @@ export default class LinkedList {
170170
}
171171

172172
delete(node) {
173-
if (!(node instanceof Node)) {
174-
throw new TypeError("node should be a valid Node instance");
175-
}
176173
this.detach(node);
177174
delete node.prev;
178175
delete node.next;

0 commit comments

Comments
 (0)