Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

basic-lru

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

basic-lru - npm Package Compare versions

Comparing version
0.0.0
to
0.0.1
+15
LICENSE
ISC License
Copyright (c) 2020, Andrea Giammarchi, @WebReflection
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
# basic-lru
[![Build Status](https://travis-ci.com/WebReflection/basic-lru.svg?branch=master)](https://travis-ci.com/WebReflection/basic-lru) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/basic-lru/badge.svg?branch=master)](https://coveralls.io/github/WebReflection/basic-lru?branch=master)
A lightweight Map based LRU implementation.
This module is a drop-in replacement for any [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) instance, and it's mostly 100% compatible with [lru](https://github.com/chriso/lru), [lru-cache](https://github.com/isaacs/node-lru-cache), [quick-lru](https://github.com/sindresorhus/quick-lru), and [lru-map](https://github.com/bchociej/lru-map) modules.
Differently from other modules, this one has the least amount of LOC, zero dependencies, and it's based on ES2015 class capability to extend the native Map.
### The Map Extend Differences
The only difference from a real Map, beside implementing a classic LRU cache, is the `peek(key)` method, to access an entry without flagging its access time anyhow, and borrowed from other libraries, plus a constructor, also borrowed from other libraries, that accepts either an integer, or an options object with `max`, or `maxSize`, and a `maxAge` property, where all of these are optional too.
+4
-7

@@ -64,3 +64,3 @@ 'use strict';

* entries() {
yield [iterator]();
yield * this[iterator]();
}

@@ -79,8 +79,5 @@ * values() {

const entries = [...super[iterator].call(this)];
const [[toBeRemoved]] = entries.sort((entry1, entry2) => {
if (entry1.time === entry2.time)
return entry1.count < entry2.count ? -1 : 1;
if (entry1.time < entry2.time)
return -1;
return 1;
const [[toBeRemoved]] = entries.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry1[prop] - entry2[prop];
});

@@ -87,0 +84,0 @@ this.delete(toBeRemoved);

@@ -63,3 +63,3 @@ const {now} = Date;

* entries() {
yield [iterator]();
yield * this[iterator]();
}

@@ -78,8 +78,5 @@ * values() {

const entries = [...super[iterator].call(this)];
const [[toBeRemoved]] = entries.sort((entry1, entry2) => {
if (entry1.time === entry2.time)
return entry1.count < entry2.count ? -1 : 1;
if (entry1.time < entry2.time)
return -1;
return 1;
const [[toBeRemoved]] = entries.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry1[prop] - entry2[prop];
});

@@ -95,2 +92,2 @@ this.delete(toBeRemoved);

}
}
};

@@ -66,3 +66,3 @@ var LRU = (function (exports) {

* entries() {
yield [iterator]();
yield * this[iterator]();
}

@@ -81,8 +81,5 @@ * values() {

const entries = [...super[iterator].call(this)];
const [[toBeRemoved]] = entries.sort((entry1, entry2) => {
if (entry1.time === entry2.time)
return entry1.count < entry2.count ? -1 : 1;
if (entry1.time < entry2.time)
return -1;
return 1;
const [[toBeRemoved]] = entries.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry1[prop] - entry2[prop];
});

@@ -89,0 +86,0 @@ this.delete(toBeRemoved);

+1
-1

@@ -1,1 +0,1 @@

var LRU=function(e){"use strict";const{now:t}=Date,{defineProperties:i}=Object,{iterator:r}=Symbol;class s extends Map{constructor(e){const t="number"==typeof e,r=(t?e:e.max||e.maxSize)||1/0,s=t?0:e.maxAge||0;i(super(),{_dropExpired:{value:this._dropExpired.bind(this)},_timer:{writable:!0,value:0},_count:{writable:!0,value:0},_max:{value:r},_maxAge:{value:s}})}get(e){const i=super.get(e);if(void 0!==i)return i.time=t(),i.value}set(e,i){const{_max:r,_maxAge:s}=this,o=0!==s;return this.size!==r||this.has(e)||(o&&this._dropExpired(),o&&this.size!==r||this._dropCount()),o&&(clearTimeout(this._timer),this._timer=setTimeout(this._dropExpired,s+1)),super.set(e,{count:this._count++,time:t(),value:i})}forEach(e,t){return super.forEach(({value:i},r)=>e.call(t,i,r,this))}peek(e){const t=super.get(e);if(void 0!==t)return t.value}*entries(){yield[r]()}*values(){for(const[e,{value:t}]of super[r].call(this))yield t}*[r](){for(const[e,{value:t}]of super[r].call(this))yield[e,t]}_dropCount(){const e=[...super[r].call(this)],[[t]]=e.sort((e,t)=>e.time===t.time?e.count<t.count?-1:1:e.time<t.time?-1:1);this.delete(t)}_dropExpired(){const e=t()-this._maxAge;super.forEach(({time:t},i)=>{t<e&&this.delete(i)})}}return e.default=s,e}({});
var LRU=function(e){"use strict";const{now:t}=Date,{defineProperties:i}=Object,{iterator:r}=Symbol;class s extends Map{constructor(e){const t="number"==typeof e,r=(t?e:e.max||e.maxSize)||1/0,s=t?0:e.maxAge||0;i(super(),{_dropExpired:{value:this._dropExpired.bind(this)},_timer:{writable:!0,value:0},_count:{writable:!0,value:0},_max:{value:r},_maxAge:{value:s}})}get(e){const i=super.get(e);if(void 0!==i)return i.time=t(),i.value}set(e,i){const{_max:r,_maxAge:s}=this,o=0!==s;return this.size!==r||this.has(e)||(o&&this._dropExpired(),o&&this.size!==r||this._dropCount()),o&&(clearTimeout(this._timer),this._timer=setTimeout(this._dropExpired,s+1)),super.set(e,{count:this._count++,time:t(),value:i})}forEach(e,t){return super.forEach(({value:i},r)=>e.call(t,i,r,this))}peek(e){const t=super.get(e);if(void 0!==t)return t.value}*entries(){yield*this[r]()}*values(){for(const[e,{value:t}]of super[r].call(this))yield t}*[r](){for(const[e,{value:t}]of super[r].call(this))yield[e,t]}_dropCount(){const e=[...super[r].call(this)],[[t]]=e.sort(([e,t],[i,r])=>{const s=t.time===r.time?"count":"time";return t[s]-r[s]});this.delete(t)}_dropExpired(){const e=t()-this._maxAge;super.forEach(({time:t},i)=>{t<e&&this.delete(i)})}}return e.default=s,e}({});
{
"name": "basic-lru",
"version": "0.0.0",
"description": "",
"version": "0.0.1",
"description": "A lightweight Map based LRU implementation",
"main": "cjs/index.js",

@@ -14,3 +14,7 @@ "scripts": {

},
"keywords": [],
"keywords": [
"lru",
"lightweight",
"map"
],
"author": "Andrea Giammarchi",

@@ -28,2 +32,2 @@ "license": "ISC",

"unpkg": "new.js"
}
}