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.1.2
to
1.0.0
+25
-25
cjs/index.js

@@ -5,2 +5,3 @@ 'use strict';

const {iterator} = Symbol;
const zero = {writable: true, value: 0};

@@ -13,9 +14,11 @@ module.exports = class LRU extends Map {

const _max = (n ? options : (options.max || options.maxSize)) || Infinity;
const _maxAge = n ? 0 : (options.maxAge || 0);
const _age = n ? 0 : (options.maxAge || 0);
defineProperties(super(), {
_dropExpired: {value: this._dropExpired.bind(this)},
_timer: {writable: true, value: 0},
_count: {writable: true, value: 0},
_dropCount: {value: this._dropCount.bind(this)},
_max: {value: _max},
_maxAge: {value: _maxAge}
_age: {value: _age},
_timer: zero,
_drop: zero,
_count: zero
});

@@ -27,3 +30,3 @@ }

const entry = super.get(key);
if (entry !== void 0) {
if (entry) {
entry.time = now();

@@ -34,13 +37,8 @@ return entry.value;

set(key, value) {
const {_max, _maxAge} = this;
const ages = _maxAge !== 0;
if (this.size === _max && !this.has(key)) {
if (ages)
this._dropExpired();
if (!ages || this.size === _max)
this._dropCount();
}
if (ages) {
const {_max, _age} = this;
if (!this._drop && this.size === _max)
this._drop = setTimeout(this._dropCount);
if (_age) {
clearTimeout(this._timer);
this._timer = setTimeout(this._dropExpired, _maxAge + 1);
this._timer = setTimeout(this._dropExpired, _age);
}

@@ -62,3 +60,3 @@ return super.set(key, {

const entry = super.get(key);
if (entry !== void 0)
if (entry)
return entry.value;

@@ -82,16 +80,18 @@ }

_dropCount() {
const entries = [...super[iterator]()];
const [[toBeRemoved]] = entries.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry1[prop] - entry2[prop];
});
this.delete(toBeRemoved);
this._drop = 0;
[...super[iterator]()]
.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry2[prop] - entry1[prop];
})
.slice(this._max)
.forEach(([key]) => this.delete(key));
}
_dropExpired() {
const expiration = now() - this._maxAge;
super.forEach(({time}, key) => {
const expiration = now() - this._age;
super.forEach(({time}, key, self) => {
if (time < expiration)
this.delete(key);
self.delete(key);
});
}
};
const {now} = Date;
const {defineProperties} = Object;
const {iterator} = Symbol;
const zero = {writable: true, value: 0};

@@ -11,9 +12,11 @@ export default class LRU extends Map {

const _max = (n ? options : (options.max || options.maxSize)) || Infinity;
const _maxAge = n ? 0 : (options.maxAge || 0);
const _age = n ? 0 : (options.maxAge || 0);
defineProperties(super(), {
_dropExpired: {value: this._dropExpired.bind(this)},
_timer: {writable: true, value: 0},
_count: {writable: true, value: 0},
_dropCount: {value: this._dropCount.bind(this)},
_max: {value: _max},
_maxAge: {value: _maxAge}
_age: {value: _age},
_timer: zero,
_drop: zero,
_count: zero
});

@@ -25,3 +28,3 @@ }

const entry = super.get(key);
if (entry !== void 0) {
if (entry) {
entry.time = now();

@@ -32,13 +35,8 @@ return entry.value;

set(key, value) {
const {_max, _maxAge} = this;
const ages = _maxAge !== 0;
if (this.size === _max && !this.has(key)) {
if (ages)
this._dropExpired();
if (!ages || this.size === _max)
this._dropCount();
}
if (ages) {
const {_max, _age} = this;
if (!this._drop && this.size === _max)
this._drop = setTimeout(this._dropCount);
if (_age) {
clearTimeout(this._timer);
this._timer = setTimeout(this._dropExpired, _maxAge + 1);
this._timer = setTimeout(this._dropExpired, _age);
}

@@ -60,3 +58,3 @@ return super.set(key, {

const entry = super.get(key);
if (entry !== void 0)
if (entry)
return entry.value;

@@ -80,16 +78,18 @@ }

_dropCount() {
const entries = [...super[iterator]()];
const [[toBeRemoved]] = entries.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry1[prop] - entry2[prop];
});
this.delete(toBeRemoved);
this._drop = 0;
[...super[iterator]()]
.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry2[prop] - entry1[prop];
})
.slice(this._max)
.forEach(([key]) => this.delete(key));
}
_dropExpired() {
const expiration = now() - this._maxAge;
super.forEach(({time}, key) => {
const expiration = now() - this._age;
super.forEach(({time}, key, self) => {
if (time < expiration)
this.delete(key);
self.delete(key);
});
}
};
+25
-25

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

const {iterator} = Symbol;
const zero = {writable: true, value: 0};

@@ -15,9 +16,11 @@ class LRU extends Map {

const _max = (n ? options : (options.max || options.maxSize)) || Infinity;
const _maxAge = n ? 0 : (options.maxAge || 0);
const _age = n ? 0 : (options.maxAge || 0);
defineProperties(super(), {
_dropExpired: {value: this._dropExpired.bind(this)},
_timer: {writable: true, value: 0},
_count: {writable: true, value: 0},
_dropCount: {value: this._dropCount.bind(this)},
_max: {value: _max},
_maxAge: {value: _maxAge}
_age: {value: _age},
_timer: zero,
_drop: zero,
_count: zero
});

@@ -29,3 +32,3 @@ }

const entry = super.get(key);
if (entry !== void 0) {
if (entry) {
entry.time = now();

@@ -36,13 +39,8 @@ return entry.value;

set(key, value) {
const {_max, _maxAge} = this;
const ages = _maxAge !== 0;
if (this.size === _max && !this.has(key)) {
if (ages)
this._dropExpired();
if (!ages || this.size === _max)
this._dropCount();
}
if (ages) {
const {_max, _age} = this;
if (!this._drop && this.size === _max)
this._drop = setTimeout(this._dropCount);
if (_age) {
clearTimeout(this._timer);
this._timer = setTimeout(this._dropExpired, _maxAge + 1);
this._timer = setTimeout(this._dropExpired, _age);
}

@@ -64,3 +62,3 @@ return super.set(key, {

const entry = super.get(key);
if (entry !== void 0)
if (entry)
return entry.value;

@@ -84,14 +82,16 @@ }

_dropCount() {
const entries = [...super[iterator]()];
const [[toBeRemoved]] = entries.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry1[prop] - entry2[prop];
});
this.delete(toBeRemoved);
this._drop = 0;
[...super[iterator]()]
.sort(([_1, entry1], [_2, entry2]) => {
const prop = entry1.time === entry2.time ? 'count' : 'time';
return entry2[prop] - entry1[prop];
})
.slice(this._max)
.forEach(([key]) => this.delete(key));
}
_dropExpired() {
const expiration = now() - this._maxAge;
super.forEach(({time}, key) => {
const expiration = now() - this._age;
super.forEach(({time}, key, self) => {
if (time < expiration)
this.delete(key);
self.delete(key);
});

@@ -98,0 +98,0 @@ }

+1
-1

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

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

@@ -24,4 +25,10 @@ "cjs": "ascjs --no-default esm cjs",

"ascjs": "^3.1.2",
"benchmark": "^2.1.4",
"bluebird": "^3.7.2",
"coveralls": "^3.0.9",
"lru": "^3.1.0",
"lru-cache": "^5.1.1",
"lru-map": "^1.6.1",
"nyc": "^15.0.0",
"quick-lru": "^4.0.1",
"rollup": "^1.31.0",

@@ -28,0 +35,0 @@ "rollup-plugin-node-resolve": "^5.2.0",

@@ -5,3 +5,3 @@ # basic-lru

A lightweight Map based LRU implementation.
A lightweight, as in 1.2K, Map based LRU implementation.

@@ -8,0 +8,0 @@ ```js