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

persistentmap

v1.1.5

Published

An ES6 Map with Redis-inspired persistence and durability.

Downloads

16

Readme

PersistentMap

An ES6 Map with Redis-inspired persistence and durability

Features:

  • Compatible - Drop-in replacement for ES6 Map
  • Performant - 100K's writes/second sustained
  • Reliable - Append-only transaction file, atomic file operations
  • Sweet and simple - Zero dependencies, pure JS, < 1KB of code

What's this for? PersitentMap is a lightweight, in-process data store that uses time-tested principles for insuring data-integrity. In short, if you want to retain metrics and analytics counters, event history, app preferences ... whatever, but don't want the hassle of setting up MySQL or Redis, this might be what you're after.

Quick Start

Install:

npm install persistentmap

Use:

// Create a map (and tell it where to save state on disk)
const pm = new PersistentMap('~/datastore.json');

// Load any prior state
await pm.load();

// Treat it like a Map
pm.set('foo', 123);
pm.get('foo'); // -> 123

// If you want to verify state has been saved before proceeding
// await the result
await pm.set('foo', 345);

// 'foo' is now saved to disk.  If/when the process dies, restarting it
// will restore 'foo' when the map is load()'ed, above

Performance

Unless you're doing 10K's or 100Ks of set() or delete() calls per second, PersistentMap performance should not be an issue. Methods that change the map state(set(), delete(), and clear()), if await'ed, will be fast, but "file system fast", not "in process memory" fast, and depend to some extent on how much data you're storing. In these cases, the Big O performance will be:

  • set(key, val): O(N) , where N = JSON.stringify(val).length
  • delete(key): O(1)
  • clear(): O(1)

For all other methods, performance should be indistuingishable from a native Map.

Note: the set() and delete() operations may occasionally trigger a full-state rewrite (this occurs when filesize > options.maxFileSize), in which case performance will be O(N), where N = JSON.stringify(map).length

API

PersistentMap extends the ES6 Map class. It provides the full ES6 Map API, with the following changes:

  • New constructor signature, documented below
  • Existing clear(), delete(), and set() methods enhanced to return a Promise that resolves when state has been successfully saved to file (or rejects on error).
  • New compact(), flush(), and load() methods, documented below

constructor(filepath, options)

  • filepath - Location of transaction file. This will be created if it does not exist. Note: PersistentMap may occasionally create a temporary file at ${filepath}.tmp, as well.
  • options.maxFileSize - Size (bytes) at which to compact the transaction file. Default = 1,000,000.

async compact()`

Saves current state of map to the transaction file.

async flush()

Wait for all pending writes to complete before resolving.

async load()

Load map state from transaction file.