{"id":18784807,"url":"https://github.com/zy445566/ncpu","last_synced_at":"2025-04-13T12:33:26.649Z","repository":{"id":57309274,"uuid":"292819093","full_name":"zy445566/ncpu","owner":"zy445566","description":"multi-threaded library that node.js run function worker","archived":false,"fork":false,"pushed_at":"2021-10-09T12:01:09.000Z","size":34,"stargazers_count":17,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-31T20:43:14.271Z","etag":null,"topics":["function","nodejs","threads","worker"],"latest_commit_sha":null,"homepage":"https://github.com/zy445566/ncpu","language":"TypeScript","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/zy445566.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-04T10:26:42.000Z","updated_at":"2023-11-03T17:13:53.000Z","dependencies_parsed_at":"2022-09-09T22:41:45.127Z","dependency_job_id":null,"html_url":"https://github.com/zy445566/ncpu","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fncpu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fncpu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fncpu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fncpu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zy445566","download_url":"https://codeload.github.com/zy445566/ncpu/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223588715,"owners_count":17169879,"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":["function","nodejs","threads","worker"],"created_at":"2024-11-07T20:44:05.664Z","updated_at":"2024-11-07T20:44:06.275Z","avatar_url":"https://github.com/zy445566.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ncpu\r\nmulti-threaded library that node.js run function worker\r\n\r\n# Installation \r\n```sh\r\nnpm install ncpu\r\n```\r\n[ncpu](https://github.com/zy445566/ncpu) for the **`node.js`** environment,use [ncpu-web](https://github.com/zy445566/ncpu-web) for the **`browser`** environment.\r\n\r\n\r\n`require:Node.js version\u003e=12`\r\n\r\n\r\n# Attention\r\nBecause it is multithreaded, context information cannot be received and parameter passing can only be achieved by cloning(\r\nThe cloning will occur as described in the [HTML structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), and an error will be thrown if the object cannot be cloned (e.g. because it contains functions)).\r\n\r\n# Quick Start\r\n```js\r\nimport {NCPU} from 'ncpu' // or const {NCPU} = require('ncpu')\r\nasync function main () {\r\n    // ### run\r\n    await NCPU.run((a,b)=\u003ea+b,[1,2]) // result: 3\r\n    await NCPU.run((list)=\u003e{\r\n        return list.reduce((total,value)=\u003e{return total+value;});\r\n    },[[1,2,3]]) // result: 6\r\n    // ### pick\r\n    const workerFibo = await NCPU.pick((num)=\u003e{\r\n        const fibo = (value)=\u003e{\r\n            if(value\u003c=2){return 1;}\r\n            return fibo(value-2)+fibo(value-1);\r\n        }\r\n        return fibo(num);\r\n    });\r\n    // slef time to run\r\n    await workerFibo(38)+await workerFibo(39) // result: 102334155 //fibo(40)\r\n    // ### getWorker // reuse a thread\r\n    const ncpuWorker = NCPU.getWorker(); \r\n    const multiplexingWorkerFibo = await NCPU.pick((num)=\u003e{\r\n        const fibo = (value)=\u003e{\r\n            if(value\u003c=2){return 1;}\r\n            return fibo(value-2)+fibo(value-1);\r\n        }\r\n        return fibo(num);\r\n    }, {ncpuWorker}); // reuse a thread\r\n    const res = await Promise.all([multiplexingWorkerFibo(38), NCPU.run((num)=\u003e{\r\n        const fibo = (value)=\u003e{\r\n            if(value\u003c=2){return 1;}\r\n            return fibo(value-2)+fibo(value-1);\r\n        }\r\n        return fibo(num);\r\n    }, [39] ,{ncpuWorker})]); // reuse a thread\r\n    // ### inject globalData\r\n    await NCPU.run(()=\u003e{\r\n        return require('fs') === require('fs');\r\n    },[],{injectList:['require']}) // result: true\r\n}\r\nmain()\r\n```\r\nThe above example spawns a Worker thread for each callback function when runing. In actual practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the overhead of creating Workers would likely exceed their benefit.\r\n\r\n# Other solutions\r\n* [pambdajs](https://github.com/tim-hub/pambdajs)\r\n* [napajs](https://github.com/microsoft/napajs)\r\n\r\n# License\r\n[ncpu](https://github.com/zy445566/ncpu) is available under the MIT license. See the [LICENSE](https://github.com/zy445566/ncpu/blob/master/LICENSE) file for details.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzy445566%2Fncpu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzy445566%2Fncpu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzy445566%2Fncpu/lists"}