npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

string-cache-map

v1.2.0

Published

WeakMap replacement to cache strings efficiently and fast

Downloads

549

Readme

StringCacheMap building status

StringCacheMap is a Map with an API compatible with WeakMap that stores string-any key-value pairs.

StringCacheMap has strings as keys and any value as a value. It is like using an object as a cache but with some advantages: 1) it is possible to limit the total of pairs key-value stored to a maximum amount, 2) default property names like "constructor" are not present, 3) it uses an additional victims cache to rescue old values and emulate a really fast LRU cache.

It is a substitute for WeakMap when keys are strings.

Quick Use

Install with npm or yarn:

npm install --save string-cache-map

Your Code:

import StringCacheMap from 'string-cache-map'

const map = new StringCacheMap(2)
map.set('a', 1)
map.set('b', 2)

console.log(map.get('a')) // output 1
console.log(map.has('a')) // output true

StringCacheMap Basic API

Add elements with set and get to obtain the value:

import StringCacheMap from 'string-cache-map'

test('set values can be get', () => {
  const map = new StringCacheMap()
  map.set('fOf', 'foo')
  map.set('bOf', 'bar')

  expect(map.get('fOf')).toBe('foo')
  expect(map.get('bOf')).toBe('bar')
})

Query if values are present with has:

import StringCacheMap from 'string-cache-map'

test('has returns true if value is present, or false otherwise', () => {
  const map = new StringCacheMap()
  map.set('fOf', 'foo')
  map.set('uOf', undefined)

  expect(map.has('fOf')).toBe(true)
  expect(map.has('uOf')).toBe(true)
  expect(map.has('bOf')).toBe(false)
  expect(map.has('constructor')).toBe(false)
  expect('constructor' in  {}).toBe(true)
})

Remove elements with delete:

import StringCacheMap from 'string-cache-map'

test('delete removes a value and returns true if was removed', () => {
  const map = new StringCacheMap()
  map.set('fOf', 'foo')

  expect(map.delete('fOf')).toBe(true)
  expect(map.delete('bOf')).toBe(false)
  expect(map.has('fOf')).toBe(false)
})

Limit

The first argument of the constructor is the limit:

import StringCacheMap from 'string-cache-map'

test('capacity is up to the double of the limit', () => {
  const map = new StringCacheMap(2)
  map.set('a', 1)
  map.set('b', 2)

  expect(map.get('a')).toBe(1)
  expect(map.has('a')).toBe(true)
})

It automatically deletes old unused values beyond the limit:

import StringCacheMap from 'string-cache-map'

test('capacity ensured is the limit', () => {
  const map = new StringCacheMap(2)
  map.set('a', 1)
  map.set('b', 2)
  map.set('c', 3)
  map.set('d', 4)
  map.set('e', 5)

  expect(map.get('d')).toBe(4)
  expect(map.has('a')).toBe(false)
})

LRU prioritizes last got values over last set values:

import StringCacheMap from 'string-cache-map'

  test('gets lru reprioritizes values', () => {
    const map = new StringCacheMap(2)
    map.set('a', 1)
    map.set('b', 2)
    map.set('c', 3)
    map.set('d', 4)
    map.get('a')
    map.get('b')
    map.get('c')
    map.set('e', 5)

    expect(map.get('d')).toBeUndefined()
    expect(map.has('d')).toBe(false)
  })

With hard mode off (second constructor argument set to false), LRU does not invalidate last set values:

import StringCacheMap from 'string-cache-map'

test('gets lru does not expulse newer values if hard is false', () => {
  const map = new StringCacheMap(2, false)
  map.set('a', 1)
  map.set('b', 2)
  map.set('c', 3)
  map.set('d', 4)
  map.get('a')
  map.get('b')
  map.get('c')
  map.set('e', 5)

  expect(map.get('d')).toBe(4)
  expect(map.has('d')).toBe(true)
})