Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
A finite key-value cache using the Least Recently Used (LRU) cache algorithm where the most recently used objects are keept in cache while less recently used items are purged.
A finite key-value cache using the Least Recently Used (LRU) cache algorithm where the most recently used objects are keept in cache while less recently used items are purged.
This implementation is compatible with most JavaScript environments (including ye olde browser) and is very efficient.
Based on a doubly-linked list for low complexity random shuffling of entries.
The cache object iself has a "head" (least recently used entry) and a "tail" (most recently used entry).
The "head" and "tail" are "entries" -- an entry might have a "newer" and an "older" entry (doubly-linked, "older" being close to "head" and "newer" being closer to "tail").
Key lookup is done through a key-entry mapping native object, which on most
platforms mean O(1)
complexity. This comes at a very low memory cost (for
storing two extra pointers for each entry).
Fancy ASCII art illustration of the general design:
entry entry entry entry
______ ______ ______ ______
| head |.newer => | |.newer => | |.newer => | tail |
| A | | B | | C | | D |
|______| <= older.|______| <= older.|______| <= older.|______|
removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
var c = new LRUCache(3);
c.put('adam', 29);
c.put('john', 26);
c.put('angela', 24);
c.toString(); // -> "adam:29 < john:26 < angela:24"
c.get('john'); // -> 26
// Now 'john' is the most recently used entry, since we just requested it
c.toString(); // -> "adam:29 < angela:24 < john:26"
c.put('zorro', 141); // -> {key:adam, value:29}
// Because we only have room for 3 entries, put-ing 'zorro' purged 'adam'
c.toString(); // -> "angela:24 < john:26 < zorro:141"
An entry is a simple Object
with at least two members: {key:Object, value:Object}
. An entry might also have a newer
member which points to a newer entry, and/or a older
member pointing to an older entry.
Creates a new cache object which will hold up to limit
entries.
Current number of entries. Read-only.
Maximum number of items this cache will keep.
Put value
into the cache associated with key
.
Returns an entry which was removed (to make room for the new entry) or undefined
if there was enough space for the new entry.
Note: The returned entry does not include any (strong) references to other entries (i.e. there is no
older
ornewer
members). This design makes garbage collection predictable.
Retrieve value for, and register recent use of, key
. Returns the value associated with key
or undefined
if not in the cache.
Remove the least recently used (oldest) entry. Returns the removed entry, or undefined
if the cache was empty.
If you need to perform any form of finalization of purged items, this is a good place to do it. Simply override/replace this function:
var c = new LRUCache(123);
c.shift = function() {
var entry = LRUCache.prototype.shift.call(this);
doSomethingWith(entry);
return entry;
}
The returned entry must not include any strong references to other entries. See note in the documentation of LRUCache.prototype.put (Object key, Object value) -> Object entry
.
Check if key
is in the cache without registering recent use. Feasible if
you do not want to chage the state of the cache, but only "peek" at it.
Returns the entry associated with key
if found, otherwise undefined
is
returned.
Note: The entry returned is managed by the cache (until purged) and thus contains members with strong references which might be altered at any time by the cache object. You should look at the returned entry as being immutable.
Update the value of entry with key
or put
a new entry. Returns the old value, or undefined if the cache was empty.
Remove entry key
from cache and return its value. Returns undefined
if key
is not found.
Removes all entries and return itself.
Return an array containing all keys of entries in arbitrary order.
Call fun
for each entry. Starting with the newest entry if desc
is a true
value, otherwise starts with the oldest (head) enrty and moves towards the
tail.
Returns nothing (undefined
).
fun
is called with 3 arguments in the context context
:
fun.call(context, Object key, Object value, LRUCache self)
Example which prints "key: value" starting with the most recent entry:
cache.forEach(function(key, value) {
puts(key+': '+value);
}, true);
Returns an array of object (for use by JSON.stringify
) of the form:
[
{key:"key1", value:"value1"},
{key:"key2", value:"value2"},
{key:"key3", value:"value3"}
]
Returns a string representation in the format:
key1:value1 < key2:value2 < key3:value3
Oldest (head) on the left hand side and newer entries to the right hand side.
As this code is most suitable for embedding, here is a shortlist of the essential parts (prototype functions) needed for a minimal implementation. All other functions, not mentioned here, are simply ancillary.
To include only the minimal code above, require lru-core.js instead of lru.js
Copyright (c) 2014 Rasmus Andersson http://hunch.se/, Ben Woosley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A finite key-value cache using the Least Recently Used (LRU) cache algorithm where the most recently used objects are keept in cache while less recently used items are purged.
The npm package js-lru receives a total of 1,508 weekly downloads. As such, js-lru popularity was classified as popular.
We found that js-lru demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.