quick-lru
Advanced tools
Comparing version 1.1.0 to 2.0.0
36
index.js
'use strict'; | ||
class QuickLRU { | ||
constructor(opts) { | ||
opts = Object.assign({}, opts); | ||
if (!(opts.maxSize && opts.maxSize > 0)) { | ||
constructor(options = {}) { | ||
if (!(options.maxSize && options.maxSize > 0)) { | ||
throw new TypeError('`maxSize` must be a number greater than 0'); | ||
} | ||
this.maxSize = opts.maxSize; | ||
this.maxSize = options.maxSize; | ||
this.cache = new Map(); | ||
@@ -65,7 +63,8 @@ this.oldCache = new Map(); | ||
delete(key) { | ||
if (this.cache.delete(key)) { | ||
const deleted = this.cache.delete(key); | ||
if (deleted) { | ||
this._size--; | ||
} | ||
this.oldCache.delete(key); | ||
return this.oldCache.delete(key) || deleted; | ||
} | ||
@@ -80,4 +79,4 @@ | ||
* keys() { | ||
for (const el of this) { | ||
yield el[0]; | ||
for (const [key] of this) { | ||
yield key; | ||
} | ||
@@ -87,4 +86,4 @@ } | ||
* values() { | ||
for (const el of this) { | ||
yield el[1]; | ||
for (const [, value] of this) { | ||
yield value; | ||
} | ||
@@ -94,9 +93,10 @@ } | ||
* [Symbol.iterator]() { | ||
for (const el of this.cache) { | ||
yield el; | ||
for (const item of this.cache) { | ||
yield item; | ||
} | ||
for (const el of this.oldCache) { | ||
if (!this.cache.has(el[0])) { | ||
yield el; | ||
for (const item of this.oldCache) { | ||
const [key] = item; | ||
if (!this.cache.has(key)) { | ||
yield item; | ||
} | ||
@@ -108,4 +108,4 @@ } | ||
let oldCacheSize = 0; | ||
for (const el of this.oldCache) { | ||
if (!this.cache.has(el[0])) { | ||
for (const key of this.oldCache.keys()) { | ||
if (!this.cache.has(key)) { | ||
oldCacheSize++; | ||
@@ -112,0 +112,0 @@ } |
{ | ||
"name": "quick-lru", | ||
"version": "1.1.0", | ||
"description": "Simple \"Least Recently Used\" (LRU) cache", | ||
"license": "MIT", | ||
"repository": "sindresorhus/quick-lru", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"lru", | ||
"quick", | ||
"cache", | ||
"caching", | ||
"least", | ||
"recently", | ||
"used", | ||
"fast", | ||
"map", | ||
"hash", | ||
"buffer" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"coveralls": "^2.12.0", | ||
"nyc": "^11.0.3", | ||
"xo": "*" | ||
} | ||
"name": "quick-lru", | ||
"version": "2.0.0", | ||
"description": "Simple \"Least Recently Used\" (LRU) cache", | ||
"license": "MIT", | ||
"repository": "sindresorhus/quick-lru", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"lru", | ||
"quick", | ||
"cache", | ||
"caching", | ||
"least", | ||
"recently", | ||
"used", | ||
"fast", | ||
"map", | ||
"hash", | ||
"buffer" | ||
], | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"coveralls": "^3.0.1", | ||
"nyc": "^13.1.0", | ||
"xo": "^0.23.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # quick-lru [![Build Status](https://travis-ci.org/sindresorhus/quick-lru.svg?branch=master)](https://travis-ci.org/sindresorhus/quick-lru) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/quick-lru/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/quick-lru?branch=master) | ||
Inspired by the [`haslru` algorithm](https://github.com/dominictarr/hashlru#algorithm), but instead uses [`Map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map) to support keys of any type, not just strings, and values can be `undefined`. | ||
Inspired by the [`hashlru` algorithm](https://github.com/dominictarr/hashlru#algorithm), but instead uses [`Map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map) to support keys of any type, not just strings, and values can be `undefined`. | ||
@@ -48,5 +48,5 @@ | ||
*Required*<br> | ||
Type: `Object` | ||
Type: `number` | ||
Maximum number of items before evicting the least recently used items. | ||
The maximum number of items before evicting the least recently used items. | ||
@@ -79,2 +79,4 @@ ### Instance | ||
Returns `true` if the item is removed or `false` if the item doesn't exist. | ||
#### .clear() | ||
@@ -81,0 +83,0 @@ |
5636
93
99