{"id":19872744,"url":"https://github.com/snuffydev/nanothreads","last_synced_at":"2025-05-02T09:31:22.263Z","repository":{"id":61330420,"uuid":"549913280","full_name":"snuffyDev/nanothreads","owner":"snuffyDev","description":"A tiny multi-threading \u0026 concurrency library, for Node.js and the browser","archived":false,"fork":false,"pushed_at":"2023-06-12T02:28:37.000Z","size":244,"stargazers_count":28,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T19:20:51.994Z","etag":null,"topics":["async","browser","concurrency","javascript","mutex","threading","threads","typescript","worker"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/snuffyDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-10-11T23:42:57.000Z","updated_at":"2024-03-17T21:32:33.000Z","dependencies_parsed_at":"2023-12-19T01:01:24.923Z","dependency_job_id":"248a2748-df7e-43ff-bc42-527c691e5628","html_url":"https://github.com/snuffyDev/nanothreads","commit_stats":{"total_commits":39,"total_committers":1,"mean_commits":39.0,"dds":0.0,"last_synced_commit":"1632d6c1d954e492f871ca59242d0dc6a6cfb880"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snuffyDev%2Fnanothreads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snuffyDev%2Fnanothreads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snuffyDev%2Fnanothreads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snuffyDev%2Fnanothreads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/snuffyDev","download_url":"https://codeload.github.com/snuffyDev/nanothreads/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252015811,"owners_count":21680828,"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":["async","browser","concurrency","javascript","mutex","threading","threads","typescript","worker"],"created_at":"2024-11-12T16:16:35.800Z","updated_at":"2025-05-02T09:31:21.921Z","avatar_url":"https://github.com/snuffyDev.png","language":"JavaScript","readme":"# nanothreads\n\nA super-fast, super-powerful Worker-based multi-threading library for the browser and Node.js.\n\nNanothreads is only ~2 KB for browsers, ~2.3 KB for Node.js, making it a _super_ tiny alternative to\n[tinypool](https://github.com/tinylibs/tinypool), [threads.js](https://github.com/andywer/threads.js) and others!\n\n**[\\[Benchmarks\\]](https://snuffydev.github.io/nanothreads/dev/bench/index.html)** |\n**[\\[Docs\\]](https://snuffydev.github.io/nanothreads/docs/index.html)**\n\n## Overview\n\n- Zero-dependencies :x:\n- Tiny bundle size! :see_no_evil:\n- 100% Fully Typed :100:\n- Super fast, super efficient :fire:\n  - Check out the [Historical Benchmarks]() or the [Benchmarks](#benchmarks) section of the README for more info\n- Works both in the browser, and Node :eyes:\n\n### Install\n\n```\nnpm install nanothreads\n\npnpm add nanothreads\n\nyarn add nanothreads\n```\n\n### Basic Usage\n\n#### Importing\n\n```ts\n// Browsers\nimport { ThreadPool } from \"nanothreads/browser\";\n\n// Node.js\nimport { ThreadPool } from \"nanothreads\";\n```\n\n\u003e Note: Browsers must specify the import path as `nanothreads/browser`.\n\n#### Kitchen Sink\n\n```ts\nimport { InlineThread, Thread, ThreadPool } from \"nanothreads\";\n\ntype Quote = {\n\tquote: string;\n};\n\n// Inline Thread\nconst inline_thread = new InlineThread\u003c[name: string], string\u003e((name) =\u003e {\n\treturn \"Hello \" + name;\n});\n\n// Thread from a script\nconst thread = new Thread\u003cnumber, number\u003e(\"./worker.ts\");\n\n// Thread Pool from an inlined function\nconst pool = new ThreadPool\u003cstring, Quote\u003e({\n\ttask: (url) =\u003e {\n\t\treturn fetch(url)\n\t\t\t.then((res) =\u003e res.json())\n\t\t\t.then((json) =\u003e json as Quote);\n\t},\n\tcount: 5, // number of threads = 5\n});\n\n// Using the thread pool\nfor (let idx = 0; idx \u003c 10; idx++) {\n\tpool.exec(\"https://api.kanye.rest\").then((quote) =\u003e {\n\t\t// output: \"{ quote: \"Man... whatever happened to my antique fish tank?\" };\"\n\t\tconsole.log(JSON.stringify(quote));\n\t});\n}\n\nconst greetings = await inline_thread.send(\"Kanye\"); // output: \"Hello Kanye\"\n\nconst my_number = await thread.send(4); // output: 8\n\n// Cleanup when done!\nawait thread.terminate();\nawait inline_thread.terminate();\nawait pool.terminate();\n```\n\n### Documentation\n\nAPI Documentation can be found here:\n[snuffydev.github.io/nanothreads/docs](https://snuffydev.github.io/nanothreads/docs/index.html), or in the `/docs`\ndirectory on GitHub.\n\n### Benchmarks\n\nYou can find the historical benchmarks [here]().\n\nProvided below is the results from my own machine (Intel i7-4700MQ, on Arch Linux):\n\n```\n## Throughput test (don't await - just execute)\nnanothreads ([inline] threadpool) x 895,733 ops/sec ±5.75% (68 runs sampled)\nnanothreads ([file] threadpool) x 932,900 ops/sec ±5.10% (69 runs sampled)\ntinypool x 355,282 ops/sec ±21.83% (50 runs sampled)\nthreads.js (threadpool) x 1,618 ops/sec ±56.60% (9 runs sampled)\n\n## Complete operations (await the result)\n\nRunning \"nanothreads inline\" suite...\nfasta x 17.85 ops/sec ±2.77% (83 runs sampled)\nRunning \"nanothreads file\" suite...\n\"fasta x 18.03 ops/sec ±2.39% (83 runs sampled)\"\nRunning \"tinypool\" suite...\n\"fasta x 9.23 ops/sec ±1.91% (47 runs sampled)\"\nRunning \"threads.js\" suite...\n\"fasta x 15.98 ops/sec ±1.59% (76 runs sampled)\"\n\nRunning \"nanothreads inline\" suite...\nfasta x 18.34 ops/sec ±2.06% (85 runs sampled)\nRunning \"nanothreads file\" suite...\n\"fasta x 18.63 ops/sec ±1.65% (86 runs sampled)\"\nRunning \"tinypool\" suite...\n\"fasta x 9.60 ops/sec ±2.02% (49 runs sampled)\"\nRunning \"threads.js\" suite...\n\"fasta x 15.29 ops/sec ±2.22% (73 runs sampled)\"\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnuffydev%2Fnanothreads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsnuffydev%2Fnanothreads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnuffydev%2Fnanothreads/lists"}