Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lru

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lru - npm Package Compare versions

Comparing version 1.2.0 to 1.2.1

61

index.js

@@ -18,20 +18,24 @@ var events = require('events');

LRU.prototype.remove = function (key) {
if (!this.cache.hasOwnProperty(key)) return;
var element = this.cache[key];
delete this.cache[key];
if(element) {
delete this.cache[key];
--this.length;
--this.length;
if(element.prev) this.cache[element.prev].next = element.next;
if(element.next) this.cache[element.next].prev = element.prev;
if(this.head == key) {
if (this.length === 0) {
this.head = this.tail = null;
} else {
if (this.head == key) {
this.head = element.prev;
}
if(this.tail == key) {
this.cache[this.head].next = null;
} else if (this.tail == key) {
this.tail = element.next;
this.cache[this.tail].prev = null;
} else {
this.cache[element.prev].next = element.next;
this.cache[element.next].prev = element.prev;
}
}
return element;

@@ -61,5 +65,6 @@ }

// Eviction is only possible if the key didn't already exist:
if (++this.length > this.max) {
if (this.length === this.max) {
this.evict();
}
++this.length;
}

@@ -83,4 +88,5 @@

LRU.prototype.get = function (key) {
if (!this.cache.hasOwnProperty(key)) return;
var element = this.cache[key];
if (!element) { return; }
if (this.maxAge && (Date.now() - element.modified) > this.maxAge) {

@@ -92,19 +98,19 @@ this.remove(key);

if( this.length > 1 ) {
// Tail only changes if this was the tail:
if( key === this.tail ) this.tail = element.next
if( this.head !== key ) {
// Set prev -> next:
if( element.prev ) this.cache[element.prev].next = element.next
// Set prevhead->next:
if( this.head ) this.cache[this.head].next = key
if( this.head !== key ) {
if (key === this.tail) {
this.tail = element.next;
this.cache[this.tail].prev = null;
} else {
// Set prev.next -> element.next:
this.cache[element.prev].next = element.next;
}
// Set next -> prev:
if( element.next ) this.cache[element.next].prev = element.prev
// Set element.next.prev -> element.prev:
this.cache[element.next].prev = element.prev;
// Set new head:
this.head = key
// Element is the new head
this.cache[this.head].next = key;
element.prev = this.head;
element.next = null;
this.head = key;
}

@@ -121,2 +127,1 @@

};
{
"name": "lru",
"description": "A simple O(1) LRU cache",
"version": "1.2.0",
"version": "1.2.1",
"author": "Chris O'Hara <cohara87@gmail.com>",

@@ -6,0 +6,0 @@ "main": "index",

@@ -38,5 +38,5 @@ # lru

Create a new LRU cache that stores `length` elements before evicting the least recently used.
Optionally you can pass a an options map with additional options instead
Optionally you can pass an options map with additional options:
``` js
```js
{

@@ -43,0 +43,0 @@ max: maxElementsToStore,

@@ -80,2 +80,12 @@ var assert = require('assert');

assert.deepEqual(['foo1','foo3'], keys(lru.cache));
},
"lru invariant is maintained after set(), get() and remove()": function() {
var lru = new LRU(2);
lru.set('a', 1);
lru.set('b', 2);
assert.deepEqual(lru.get('a'), 1);
lru.remove('a');
lru.set('c', 1);
lru.set('d', 1);
assert.deepEqual(['c','d'], keys(lru.cache));
}

@@ -82,0 +92,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc