
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
A simple abstraction for Node.js worker_threads
, allowing you to create and share a Map (hash table)
between worker threads and the main process. This simplifies the process of managing shared data and communication between worker threads.
Under the hood, the library uses SharedArrayBuffer
to create shared memory, enabling seamless data sharing between threads. Additionally, it uses Atomics
mechanism to implement a mutex, ensuring thread safety and preventing race conditions during data access and manipulation.
npm i worker_map
First, let's create a simple hash map structure in main process, then create a worker thread and share the hash.
// main.js
const { Worker } = require("worker_threads");
const { WorkerMap } = require("worker_map");
const map = new WorkerMap();
map.set("balance", 100); // sync operation
new Worker("./worker.js", {
workerData: {
mapBuffer: map.toSharedBuffer(),
},
});
setTimeout(() => {
console.log(map.get("balance")); // 200
}, 50);
Now, let's access the shared hash map structure in the worker thread.
// worker.js
const { WorkerMap } = require("worker_map");
const { workerData } = require("worker_threads");
const map = new WorkerMap(workerData.mapBuffer);
console.log(map.get("balance")); // 100
// The change will be reflected in the main process as well
map.set("balance", 200);
Worker_map is much like JavaScript's regular Map.
map.set(key, value)
map.set('name', 'John'); // true
map.get(key):
const name = map.get('name'); // 'John'
map.delete(key):
map.delete('name'); // true
map.delete('something'); // false because it doesn't exist
map.clear():
map.clear();
map.size(); // 0
map.has(key)
map.has('name'); // true
map.has('country'); // false
map.size()
map.has('size'); // 1
map.keys()
map.keys(); // [ 'name' ]
map.entries()
for (const [ key, value ] of map.entries()) {
console.log(`${key}: ${value}`); // name: 'John'
}
map.forEach()
map.forEach(function(key, value, map) {
console.log(`${key}: ${value}`); // name: 'John'
});
map.toSharedBuffer()
const buffer = map.toSharedBuffer();
const sameMap = new WorkerMap(buffer);
map.toObject()
const mapObject = map.toObject(); // { ... }
mapObject.name; // 'John'
See the contributing guide for detailed instructions on how to get started with our project.
TODO
Please be aware of the following limitations when using our library:
FAQs
Tread-safe map structure for worker_threads.
The npm package worker_map receives a total of 11 weekly downloads. As such, worker_map popularity was classified as not popular.
We found that worker_map 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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.