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

native-keyshare

v1.1.2

Published

High-performance native shared object store for Node.js worker threads

Downloads

885

Readme

native-keyshare

A high-performance shared key-value store implementation designed for multi-threaded environments. This library leverages SharedArrayBuffer to enable efficient data sharing between worker threads.

Features

  • Shared Data Access: Allows multiple worker threads to share and manipulate data using named stores
  • High Performance: Optimized for fast reads and writes using TypedArrays and efficient buffer handling
  • Pattern Operations: Built-in support for wildcards and regex patterns
  • Simplified Communication: Uses BroadcastChannel for seamless thread communication
  • No Dependencies: Core functionality works without external dependencies
  • Optional msgpackr: 2-4x performance boost when using msgpackr

Prerequisites

  • Node.js >= 16.0.0
  • msgpackr (optional): Improves serialization/deserialization performance by 2-4x

Installation

Install the library using npm:

npm install native-keyshare

If you want to enable performance improvements, also install msgpackr:

npm install msgpackr

Usage

Basic Operations

const { createStore } = require('native-keyshare');

// Create a named store
const store = createStore('mystore');

// Basic operations
store.set('user:1', { name: 'John' });
console.log(store.get('user:1'));  // { name: 'John' }
store.delete('user:1');

// Named stores are isolated
const cacheStore = createStore('cache');
const userStore = createStore('users');

// Get same store instance
const sameStore = createStore('mystore');

Worker Thread Example

Main Thread (index.js)

const { Worker } = require('worker_threads');
const { createStore } = require('native-keyshare');

const store = createStore('shared');
store.set('sharedKey', { value: 'Hello from main!' });

const worker1 = new Worker('./worker.js');
const worker2 = new Worker('./worker.js');

Worker Thread (worker.js)

const { createStore } = require('native-keyshare');

const store = createStore('shared');
console.log(store.get('sharedKey'));  // { value: 'Hello from main!' }
store.set('workerKey', { data: 'Hello from worker!' });

API

Store Creation

createStore(storeName?: string)

Creates or retrieves a named key-value store instance.

const store = createStore('mystore');    // Named store
const defaultStore = createStore();      // Default store

Store Methods

set(key: string, value: any, options?: Options): boolean

Sets a key-value pair in the store.

  • options.minBufferSize: Initial buffer size in bytes if you expect value to grow
  • options.immutable: Dont allow rewriting the buffer. create a new one on update.
  • options.ttl: TTL in seconds.

get(key: string): any

Retrieves a value from the store.

delete(key: string): boolean

Deletes a value. Supports patterns.

listKeys(pattern?: string): string[]

Lists all keys, optionally filtered by pattern.

clear(): void

Clear the store.

close(): void

Close the store. cleanup local maps and buffer references.

Pattern Operations

The store supports two pattern matching styles for delete() and listKeys():

// Glob-style wildcards
store.delete('users:*');     // Matches: users:123, users:abc, etc
store.delete('session:?');   // Matches: session:1, session:a
store.delete('cache:??');    // Matches: cache:12, cache:ab

// Regular expressions (enclosed in forward slashes)
store.delete('/^user_\d+$/');   // Matches: user_1, user_123
store.delete('/test_.+/');      // Matches: test_abc, test_123

// List keys matching patterns
const userKeys = store.listKeys('user:*');
const logKeys = store.listKeys('/log_\d+/');

Performance Tips

  1. Use msgpackr for better serialization (2-4x faster)
  2. Set appropriate minBufferSize when you know data will grow
  3. Batch operations when possible instead of individual calls
  4. Use pattern operations sparingly on large stores

Benchmarks

Performance test for get:

const store = createStore();
store.set('test', { value: 'Benchmark' });

console.time('Benchmark');
for (let i = 0; i < 10000000; i++) {
  store.get('test');
}
console.timeEnd('Benchmark');