https://github.com/jaaamesey/parra
a `run-with-worker` wrapper for easy parallel computation in JavaScript
https://github.com/jaaamesey/parra
javascript parallelization web-workers
Last synced: 6 months ago
JSON representation
a `run-with-worker` wrapper for easy parallel computation in JavaScript
- Host: GitHub
- URL: https://github.com/jaaamesey/parra
- Owner: jaaamesey
- Created: 2025-03-02T13:26:04.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-03-02T14:15:12.000Z (10 months ago)
- Last Synced: 2025-03-02T15:24:59.791Z (10 months ago)
- Topics: javascript, parallelization, web-workers
- Language: TypeScript
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# parra: parallel computations made easy
*Note: This library is still in a very experimental stage of development. Feel free to report any issues [here](https://github.com/jaaamesey/parra/issues).*
This library wraps [run-with-worker](https://github.com/jaaamesey/run-with-worker) with Array functions that enable easy, safe, and platform-agnostic parallel computations in JavaScript Web Workers.
It currently supports:
1. a parallel `Array.map` equivalent
2. a parallel `Array.reduce` equivalent
As with the core run-with-worker package, the amount of overhead Web Workers introduce makes parallelization only useful for computations that would take more than a few milliseconds.
## Examples
```ts
test("parallelMap: powers of 2", async () => {
const res = await parallelMap(
4,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
(item, [base]) => base ** item,
[2],
);
expect(res).toEqual([2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]);
});
test("parallelReduce: sum of numbers", async () => {
const res = await parallelReduce(
4,
Array.from({ length: 100 }).map((_, i) => i + 1),
(acc, item) => acc + item,
0,
[],
);
expect(res).toEqual(5050);
});
test("parallelReduce: product of numbers", async () => {
const res = await parallelReduce(
4,
Array.from({ length: 10 }).map((_, i) => i + 1),
(acc, item) => acc * item,
1,
[],
);
expect(res).toEqual(3628800);
});
```