# ncpu-web

> multi-threaded library that browser run function worker

Latest version **2.0.0** (published 2025-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ncpu-web
pnpm add ncpu-web
yarn add ncpu-web
bun add ncpu-web
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-09-28 |
| First published | 2020-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 55.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | zy445566 |
| Maintainers | zy445566 |
| Keywords | web, browser, function, worker, threads |

## Links

- npm: https://www.npmjs.com/package/ncpu-web
- Repository: https://github.com/zy445566/ncpu-web
- Homepage: https://github.com/zy445566/ncpu-web#readme
- Issues: https://github.com/zy445566/ncpu-web/issues
- npm.io page: https://npm.io/package/ncpu-web

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2025-09-28
- 1.1.10 — 2020-10-30
- 1.1.9 — 2020-10-29
- 1.1.8 — 2020-10-29
- 1.1.7 — 2020-10-29
- 1.1.6 — 2020-10-29
- 1.1.5 — 2020-10-29
- 1.1.4 — 2020-10-28
- 1.1.3 — 2020-10-28
- 1.1.2 — 2020-10-23
- 1.1.1 — 2020-10-23
- 1.0.3 — 2020-09-10
- 1.0.2 — 2020-09-09
- 1.0.1 — 2020-09-08
- 1.0.0 — 2020-09-07

## README

# ncpu-web
multi-threaded library that browser run function worker

# Installation 
```sh
npm install ncpu-web
```
[ncpu-web](https://github.com/zy445566/ncpu-web) for the **`browser`** environment,use [ncpu](https://github.com/zy445566/ncpu) for the **`node.js`** environment.


`require:Chrome60+，FireFox57+`

# Attention
Because it is multithreaded, context information cannot be received and parameter passing can only be achieved by cloning(
The 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)).

# Quick Start
```js
import {NCPU} from 'ncpu-web' // or const {NCPU} = require('ncpu-web')
async function main () {
  // ### run
    await NCPU.run((a,b)=>a+b,[1,2]) // result: 3
    await NCPU.run((list)=>{
        return list.reduce((total,value)=>{return total+value;});
    },[[1,2,3]]) // result: 6
  // ### pick
    const workerFibo = await NCPU.pick((num)=>{
        const fibo = (value)=>{
            if(value<=2){return 1;}
            return fibo(value-2)+fibo(value-1);
        }
        return fibo(num);
    });
    // slef time to run
    await workerFibo(38)+await workerFibo(39) // result: 102334155 //fibo(40)
    // ### getWorkerPool // reuse a thread
    const ncpuWorkerPool = NCPU.getWorkerPool(); 
    const multiplexingWorkerFibo = await NCPU.pick((num)=>{
        const fibo = (value)=>{
            if(value<=2){return 1;}
            return fibo(value-2)+fibo(value-1);
        }
        return fibo(num);
    }, {ncpuWorkerPool}); // reuse a thread
    const res = await Promise.all([multiplexingWorkerFibo(38), NCPU.run((num)=>{
        const fibo = (value)=>{
            if(value<=2){return 1;}
            return fibo(value-2)+fibo(value-1);
        }
        return fibo(num);
    }, [39] ,{ncpuWorkerPool})]); // reuse a thread
    
    // use the default thread pool
    const defaultPool = NCPU.getDefaultWorkerPool();
    await NCPU.run((a, b) => a + b, [5, 10], {ncpuWorkerPool: defaultPool}); // result: 15
}
main()
```
The 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.

# License
[ncpu-web](https://github.com/zy445566/ncpu-web) is available under the MIT license. See the [LICENSE](https://github.com/zy445566/ncpu-web/blob/master/LICENSE) file for details.

---
_Source: https://npm.io/package/ncpu-web · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
