Skip to content

Commit 083f73a

Browse files
committed
Performance improvement
1 parent 1710557 commit 083f73a

14 files changed

+66
-69
lines changed

README.md

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

4444
// Check data exists in cache
45-
cache.has("a");
45+
cache.has("a"); // true
4646

4747
// Get data from cache
48-
console.log(cache.get("a"));
48+
console.log(cache.get("a")); // 10
4949

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

5555
// Get all data to array
56-
console.log(cache.toArray());
56+
console.log(cache.toArray()); // [ { a: 10 } ]
5757

5858
// Delete data from cache
5959
cache.delete("a");
@@ -62,25 +62,28 @@ cache.delete("a");
6262
cache.clear();
6363
```
6464

65-
## Create a new cache object
65+
## Create a new cache
6666

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

6969
Defination:
70+
7071
```js
7172
const cache = new Cache(options);
7273
```
7374

7475
Where options are the following:
76+
7577
- `evictionPolicy` : eviction policy is can be any valid cache eviction policy, supported eviction policy are FIFO, LIFO, LRU, MRU
7678
- `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.
7779
- `ttl` : is cache expires time in milliseconds, the default value is 0 and if value if 0 it will not check the ttl.
78-
- `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.
80+
- `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.
7981
- `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.
8082

81-
Cachejs 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.
83+
Cachejs 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.
8284

8385
Example:
86+
8487
```js
8588
const Cache = require("@opensnip/cachejs");
8689

@@ -97,7 +100,7 @@ const cache = new Cache({
97100

98101
In cachejs 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.
99102

100-
```js
103+
````js
101104
// Add new data in cache
102105
cache.set("a", 10);
103106

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

package.json

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

src/cache.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports = class Cache {
126126

127127
this.#config.intervalId = setInterval(
128128
function (cache) {
129-
if (cache.length == 0) return;
129+
if (cache.length === 0) return;
130130
cache.forEach(function (data) {
131131
// Automatically invalidate expired cache
132132
});

src/cache.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export default class Cache {
126126

127127
this.#config.intervalId = setInterval(
128128
function (cache) {
129-
if (cache.length == 0) return;
129+
if (cache.length === 0) return;
130130
cache.forEach(function (data) {
131131
// Automatically invalidate expired cache
132132
});

src/fifo.cjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = class FIFO {
7070
// Insert a new node at head
7171
const existingNode = this.#cache.get(key);
7272
// Update node data if node is already exists
73-
if (existingNode instanceof Node) {
73+
if (typeof existingNode !== "undefined") {
7474
existingNode.value = nodeValue;
7575
} else {
7676
// Remove node if cache is full
@@ -91,7 +91,7 @@ module.exports = class FIFO {
9191

9292
const node = this.#cache.get(key);
9393

94-
if (node instanceof Node) {
94+
if (typeof node !== "undefined") {
9595
// Check node is live or not
9696
if (this.#isStale(node)) {
9797
this.delete(key);
@@ -118,15 +118,15 @@ module.exports = class FIFO {
118118
delete(key) {
119119
const node = this.#cache.get(key);
120120

121-
if (node instanceof Node) {
121+
if (typeof node !== "undefined") {
122122
this.#linkedList.delete(node);
123123
// Delete node
124124
this.#cache.delete(key);
125125
}
126126
}
127127

128128
#evict() {
129-
if (this.#linkedList.tail == null) return;
129+
if (this.#linkedList.tail === null) return;
130130
if (this.length !== this.#maxLength) return;
131131
this.delete(this.#linkedList.tail.value.key);
132132
}
@@ -140,7 +140,7 @@ module.exports = class FIFO {
140140
has(key) {
141141
const node = this.#cache.get(key);
142142

143-
if (node instanceof Node) {
143+
if (typeof node !== "undefined") {
144144
// Check node is live or not
145145
if (this.#isStale(node)) {
146146
this.delete(key);

src/fifo.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class FIFO {
7070
// Insert a new node at head
7171
const existingNode = this.#cache.get(key);
7272
// Update node data if node is already exists
73-
if (existingNode instanceof Node) {
73+
if (typeof existingNode !== "undefined") {
7474
existingNode.value = nodeValue;
7575
} else {
7676
// Remove node if cache is full
@@ -91,7 +91,7 @@ export default class FIFO {
9191

9292
const node = this.#cache.get(key);
9393

94-
if (node instanceof Node) {
94+
if (typeof node !== "undefined") {
9595
// Check node is live or not
9696
if (this.#isStale(node)) {
9797
this.delete(key);
@@ -118,15 +118,15 @@ export default class FIFO {
118118
delete(key) {
119119
const node = this.#cache.get(key);
120120

121-
if (node instanceof Node) {
121+
if (typeof node !== "undefined") {
122122
this.#linkedList.delete(node);
123123
// Delete node
124124
this.#cache.delete(key);
125125
}
126126
}
127127

128128
#evict() {
129-
if (this.#linkedList.tail == null) return;
129+
if (this.#linkedList.tail === null) return;
130130
if (this.length !== this.#maxLength) return;
131131
this.delete(this.#linkedList.tail.value.key);
132132
}
@@ -140,7 +140,7 @@ export default class FIFO {
140140
has(key) {
141141
const node = this.#cache.get(key);
142142

143-
if (node instanceof Node) {
143+
if (typeof node !== "undefined") {
144144
// Check node is live or not
145145
if (this.#isStale(node)) {
146146
this.delete(key);

src/lifo.cjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = class LIFO {
7070
// Insert a new node at head
7171
const existingNode = this.#cache.get(key);
7272
// Update node data if node is already exists
73-
if (existingNode instanceof Node) {
73+
if (typeof existingNode !== "undefined") {
7474
existingNode.value = nodeValue;
7575
} else {
7676
// Remove node if cache is full
@@ -91,7 +91,7 @@ module.exports = class LIFO {
9191

9292
const node = this.#cache.get(key);
9393

94-
if (node instanceof Node) {
94+
if (typeof node !== "undefined") {
9595
// Check node is live or not
9696
if (this.#isStale(node)) {
9797
this.delete(key);
@@ -118,15 +118,15 @@ module.exports = class LIFO {
118118
delete(key) {
119119
const node = this.#cache.get(key);
120120

121-
if (node instanceof Node) {
121+
if (typeof node !== "undefined") {
122122
this.#linkedList.delete(node);
123123
// Delete node
124124
this.#cache.delete(key);
125125
}
126126
}
127127

128128
#evict() {
129-
if (this.#linkedList.head == null) return;
129+
if (this.#linkedList.head === null) return;
130130
if (this.length !== this.#maxLength) return;
131131
this.delete(this.#linkedList.head.value.key);
132132
}
@@ -140,7 +140,7 @@ module.exports = class LIFO {
140140
has(key) {
141141
const node = this.#cache.get(key);
142142

143-
if (node instanceof Node) {
143+
if (typeof node !== "undefined") {
144144
// Check node is live or not
145145
if (this.#isStale(node)) {
146146
this.delete(key);

src/lifo.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class LIFO {
7070
// Insert a new node at head
7171
const existingNode = this.#cache.get(key);
7272
// Update node data if node is already exists
73-
if (existingNode instanceof Node) {
73+
if (typeof existingNode !== "undefined") {
7474
existingNode.value = nodeValue;
7575
} else {
7676
// Remove node if cache is full
@@ -91,7 +91,7 @@ export default class LIFO {
9191

9292
const node = this.#cache.get(key);
9393

94-
if (node instanceof Node) {
94+
if (typeof node !== "undefined") {
9595
// Check node is live or not
9696
if (this.#isStale(node)) {
9797
this.delete(key);
@@ -118,15 +118,15 @@ export default class LIFO {
118118
delete(key) {
119119
const node = this.#cache.get(key);
120120

121-
if (node instanceof Node) {
121+
if (typeof node !== "undefined") {
122122
this.#linkedList.delete(node);
123123
// Delete node
124124
this.#cache.delete(key);
125125
}
126126
}
127127

128128
#evict() {
129-
if (this.#linkedList.head == null) return;
129+
if (this.#linkedList.head === null) return;
130130
if (this.length !== this.#maxLength) return;
131131
this.delete(this.#linkedList.head.value.key);
132132
}
@@ -140,7 +140,7 @@ export default class LIFO {
140140
has(key) {
141141
const node = this.#cache.get(key);
142142

143-
if (node instanceof Node) {
143+
if (typeof node !== "undefined") {
144144
// Check node is live or not
145145
if (this.#isStale(node)) {
146146
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)