{"id":25006914,"url":"https://github.com/nairihar/worker_map","last_synced_at":"2025-04-12T16:09:47.407Z","repository":{"id":192135140,"uuid":"683498085","full_name":"nairihar/worker_map","owner":"nairihar","description":"🧵 Tread-safe map structure for worker_threads.","archived":false,"fork":false,"pushed_at":"2023-10-16T16:21:25.000Z","size":102,"stargazers_count":26,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T02:36:11.961Z","etag":null,"topics":["map","threads","worker","worker-threads"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nairihar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-26T18:56:53.000Z","updated_at":"2024-08-26T17:24:30.000Z","dependencies_parsed_at":"2024-10-30T01:33:03.571Z","dependency_job_id":null,"html_url":"https://github.com/nairihar/worker_map","commit_stats":null,"previous_names":["nairihar/threadshare"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Fworker_map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Fworker_map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Fworker_map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Fworker_map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nairihar","download_url":"https://codeload.github.com/nairihar/worker_map/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248594141,"owners_count":21130312,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["map","threads","worker","worker-threads"],"created_at":"2025-02-05T01:52:45.138Z","updated_at":"2025-04-12T16:09:47.378Z","avatar_url":"https://github.com/nairihar.png","language":"JavaScript","readme":"![](https://img.shields.io/badge/dependencies-none-brightgreen.svg)\n![](https://img.shields.io/npm/l/worker_map.svg)\n![](https://img.shields.io/npm/dt/worker_map.svg)\n\n# worker_map\n\nA 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.\n\n![](https://topentol.sirv.com/github/worker_map.jpg)\n\nUnder 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.\n\n## Installation\n\n```\nnpm i worker_map\n```\n\n## Basic Example\n\nFirst, let's create a simple `hash map` structure in the main process, then create a worker thread and share the `hash map`.\n\n```js\n// main.js\nconst { Worker } = require('worker_threads');\nconst { WorkerMap } = require('worker_map');\n\nconst map = new WorkerMap();\nmap.set('balance', 100); // sync operation\n\nnew Worker('./worker.js', {\n  workerData: {\n    mapBuffer: map.toSharedBuffer(),\n  },\n});\n\nsetTimeout(() =\u003e {\n  console.log(map.get('balance')); // 200\n}, 50);\n```\n\nNow, let's access the shared `hash map` from the worker thread.\n\n```js\n// worker.js\nconst { WorkerMap } = require('worker_map');\nconst { workerData } = require('worker_threads');\n\nconst map = new WorkerMap(workerData.mapBuffer);\nconsole.log(map.get('balance')); // 100\n\n// The change will be reflected in the main process as well\nmap.set('balance', 200);\n```\n\n## Instance methods\n\nWorker_map is much like JavaScript's regular Map.\n\n### `map.set(key, value)`\n\n```js\nmap.set('name', 'John'); // true\n```\n\n### `map.get(key):`\n\n```js\nconst name = map.get('name'); // 'John'\n```\n\n### `map.delete(key):`\n\n```js\nmap.delete('name'); // true\nmap.delete('something'); // false because it doesn't exist\n```\n\n### `map.clear():`\n\n```js\nmap.clear();\nmap.size(); // 0\n```\n\n### `map.has(key)`\n\n```js\nmap.has('name'); // true\nmap.has('country'); // false\n```\n\n### `map.size()`\n\n```js\nmap.has('size'); // 1\n```\n\n### `map.keys()`\n\n```js\nmap.keys(); // [ 'name' ]\n```\n\n### `map.entries()`\n\n```js\nfor (const [ key, value ] of map.entries()) {\n  console.log(`${key}: ${value}`); // name: 'John'\n}\n```\n\n### `map.forEach()`\n\n```js\nmap.forEach(function(key, value, map) {\n  console.log(`${key}: ${value}`); // name: 'John'\n});\n```\n\n### `map.toSharedBuffer()`\n\n```js\nconst buffer = map.toSharedBuffer();\nconst sameMap = new WorkerMap(buffer);\n```\n\n### `map.toObject()`\n\n```js\nconst mapObject = map.toObject(); // { ... }\nmapObject.name; // 'John'\n```\n\n## Contributing\n\nSee the [contributing guide](https://github.com/nairihar/worker_map/blob/main/CONTRIBUTING.md) for detailed instructions on how to get started with our project.\n\n**TODO**\n\n- Currently, when performing an action on the map, it temporarily locks the entire map, loads the necessary data, and then unlocks the map, allowing other threads to access it. However, this approach is suboptimal. It would be more efficient if we could lock only the specific portion of memory required for the particular operation.\n\n## Limitations\n\nPlease be aware of the following limitations when using our library:\n\n1. **Functions:** Function types are not supported.\n2. **NaN Values:** NaN values are not supported.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnairihar%2Fworker_map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnairihar%2Fworker_map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnairihar%2Fworker_map/lists"}