stardust-parallel-js 1.0.10 β 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +306 -310
- package/README.ru.md +309 -311
- package/dist/primitives/Thread.d.ts.map +1 -1
- package/dist/primitives/Thread.js +7 -6
- package/dist/primitives/Thread.js.map +1 -1
- package/dist/primitives/ThreadPool.d.ts.map +1 -1
- package/dist/primitives/ThreadPool.js +9 -7
- package/dist/primitives/ThreadPool.js.map +1 -1
- package/dist/utils/extractTransferables.d.ts +3 -0
- package/dist/utils/extractTransferables.d.ts.map +1 -0
- package/dist/utils/extractTransferables.js +24 -0
- package/dist/utils/extractTransferables.js.map +1 -0
- package/dist/utils/workerFactory.d.ts +5 -2
- package/dist/utils/workerFactory.d.ts.map +1 -1
- package/dist/utils/workerFactory.js +47 -42
- package/dist/utils/workerFactory.js.map +1 -1
- package/package.json +16 -9
- package/dist/Thread.d.ts +0 -8
- package/dist/Thread.d.ts.map +0 -1
- package/dist/Thread.js +0 -39
- package/dist/Thread.js.map +0 -1
- package/dist/ThreadPool.d.ts +0 -22
- package/dist/ThreadPool.d.ts.map +0 -1
- package/dist/ThreadPool.js +0 -95
- package/dist/ThreadPool.js.map +0 -1
- package/dist/examples/thread.d.ts +0 -2
- package/dist/examples/thread.d.ts.map +0 -1
- package/dist/examples/thread.js +0 -60
- package/dist/examples/thread.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,310 +1,306 @@
|
|
|
1
|
-
# β‘ stardust-parallel-js
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/stardust-parallel-js)
|
|
4
|
-
[](https://www.npmjs.com/package/stardust-parallel-js)
|
|
5
|
-
[](https://opensource.org/licenses/MIT)
|
|
6
|
-
[](https://github.com/b1411/parallel.js)
|
|
7
|
-
[](https://nodejs.org)
|
|
8
|
-
[](https://www.typescriptlang.org/)
|
|
9
|
-
[](https://bundlephobia.com/package/stardust-parallel-js)
|
|
10
|
-
[](https://github.com/b1411/parallel.js)
|
|
11
|
-
|
|
12
|
-
>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
# or
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
console.log(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
-
|
|
239
|
-
-
|
|
240
|
-
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
-
|
|
253
|
-
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
- [ ] Support for
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
##
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
<p align="center">
|
|
309
|
-
Made with β€οΈ for the Node.js community
|
|
310
|
-
</p>
|
|
1
|
+
# β‘ stardust-parallel-js
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/stardust-parallel-js)
|
|
4
|
+
[](https://www.npmjs.com/package/stardust-parallel-js)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://github.com/b1411/parallel.js)
|
|
7
|
+
[](https://nodejs.org)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
[](https://bundlephobia.com/package/stardust-parallel-js)
|
|
10
|
+
[](https://github.com/b1411/parallel.js)
|
|
11
|
+
|
|
12
|
+
> **Other languages:** [Π ΡΡΡΠΊΠΈΠΉ](./README.ru.md)
|
|
13
|
+
|
|
14
|
+
A library for parallel execution of JavaScript/TypeScript functions using Worker Threads in Node.js.
|
|
15
|
+
|
|
16
|
+
## Performance
|
|
17
|
+
|
|
18
|
+
Benchmarks on 4-core CPU:
|
|
19
|
+
|
|
20
|
+
| Task | Sequential | Parallel (4 workers) | Speedup |
|
|
21
|
+
|------|-----------|---------------------|---------|
|
|
22
|
+
| **Fibonacci(35-42) computation** | 5113 ms | 2606 ms | **1.96x** |
|
|
23
|
+
| **Processing 50 items** | 936 ms | 344 ms | **2.72x** |
|
|
24
|
+
|
|
25
|
+
> Performance improvement: up to 63% on CPU-intensive tasks.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- Real speedup on multi-core processors
|
|
30
|
+
- Simple API for parallel task execution
|
|
31
|
+
- Thread pool for efficient resource management
|
|
32
|
+
- Single thread support for one-off tasks
|
|
33
|
+
- Full TypeScript support
|
|
34
|
+
- Automatic recovery of crashed threads
|
|
35
|
+
- Array processing similar to `map()`, but parallel
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install stardust-parallel-js
|
|
41
|
+
# or
|
|
42
|
+
pnpm install stardust-parallel-js
|
|
43
|
+
# or
|
|
44
|
+
yarn add stardust-parallel-js
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quick Start
|
|
48
|
+
|
|
49
|
+
Basic usage example:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { ThreadPool } from 'stardust-parallel-js';
|
|
53
|
+
|
|
54
|
+
const pool = new ThreadPool(4);
|
|
55
|
+
|
|
56
|
+
// Sequential execution
|
|
57
|
+
const results = data.map(item => heavyComputation(item));
|
|
58
|
+
|
|
59
|
+
// Parallel execution
|
|
60
|
+
const results = await pool.map(data, item => heavyComputation(item));
|
|
61
|
+
|
|
62
|
+
await pool.terminate();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
### ThreadPool - Thread pool (recommended)
|
|
68
|
+
|
|
69
|
+
Use `ThreadPool` to process multiple tasks with maximum efficiency:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { ThreadPool } from 'stardust-parallel-js';
|
|
73
|
+
|
|
74
|
+
// Create a pool of 4 threads (matching CPU cores)
|
|
75
|
+
const pool = new ThreadPool(4);
|
|
76
|
+
|
|
77
|
+
// Process array in parallel
|
|
78
|
+
const numbers = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
79
|
+
const squares = await pool.map(numbers, (n: number) => n * n);
|
|
80
|
+
console.log(squares); // [1, 4, 9, 16, 25, 36, 49, 64]
|
|
81
|
+
|
|
82
|
+
// CPU-intensive computations
|
|
83
|
+
const result = await pool.execute(
|
|
84
|
+
(n: number) => {
|
|
85
|
+
let sum = 0;
|
|
86
|
+
for (let i = 0; i < n; i++) {
|
|
87
|
+
sum += Math.sqrt(i);
|
|
88
|
+
}
|
|
89
|
+
return sum;
|
|
90
|
+
},
|
|
91
|
+
[1000000]
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// Release resources
|
|
95
|
+
await pool.terminate();
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Thread - Single thread (for simple tasks)
|
|
99
|
+
|
|
100
|
+
Use `Thread` for one-off operations:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { Thread } from 'stardust-parallel-js';
|
|
104
|
+
|
|
105
|
+
// Start and wait for result
|
|
106
|
+
const thread = new Thread(
|
|
107
|
+
(text: string) => text.toUpperCase(),
|
|
108
|
+
['hello world']
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const result = await thread.join();
|
|
112
|
+
console.log(result); // "HELLO WORLD"
|
|
113
|
+
|
|
114
|
+
// Arrow functions work!
|
|
115
|
+
const thread2 = new Thread(x => x * 2, [21]);
|
|
116
|
+
console.log(await thread2.join()); // 42
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Examples
|
|
120
|
+
|
|
121
|
+
### Image Processing
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import { ThreadPool } from 'stardust-parallel-js';
|
|
125
|
+
|
|
126
|
+
const pool = new ThreadPool(8);
|
|
127
|
+
const images = ['img1.jpg', 'img2.jpg', /* ... */ 'img100.jpg'];
|
|
128
|
+
|
|
129
|
+
// Process 100 images in parallel
|
|
130
|
+
const processed = await pool.map(images, (path: string) => {
|
|
131
|
+
const fs = require('fs');
|
|
132
|
+
const sharp = require('sharp');
|
|
133
|
+
// Complex image processing
|
|
134
|
+
return processImage(path);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await pool.terminate();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Parsing Large Data
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
const pool = new ThreadPool(4);
|
|
144
|
+
const chunks = splitDataIntoChunks(bigData, 1000);
|
|
145
|
+
|
|
146
|
+
// Parse each chunk in parallel
|
|
147
|
+
const parsed = await pool.map(chunks, (chunk: any[]) => {
|
|
148
|
+
return chunk.map(item => parseComplexData(item));
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await pool.terminate();
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Calculations and Analytics
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const pool = new ThreadPool(4);
|
|
158
|
+
|
|
159
|
+
const results = await pool.map([35, 36, 37, 38, 39, 40], n => {
|
|
160
|
+
function fibonacci(num: number): number {
|
|
161
|
+
if (num <= 1) return num;
|
|
162
|
+
return fibonacci(num - 1) + fibonacci(num - 2);
|
|
163
|
+
}
|
|
164
|
+
return fibonacci(n);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
await pool.terminate();
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Benchmarks
|
|
171
|
+
|
|
172
|
+
Run benchmarks:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
npm run build
|
|
176
|
+
npx tsx benchmarks/cpu-intensive.ts
|
|
177
|
+
npx tsx benchmarks/data-processing.ts
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## API Reference
|
|
181
|
+
|
|
182
|
+
### ThreadPool
|
|
183
|
+
|
|
184
|
+
#### `constructor(size: number)`
|
|
185
|
+
Creates a thread pool of the specified size.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
const pool = new ThreadPool(4);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### `execute<TArgs, TResult>(fn: (...args: TArgs) => TResult, args?: TArgs): Promise<TResult>`
|
|
192
|
+
Executes a function in an available thread from the pool.
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
const result = await pool.execute((x: number) => x * x, [5]);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### `map<T, R>(items: T[], fn: (item: T) => R): Promise<R[]>`
|
|
199
|
+
Applies a function to each array element in parallel.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// Arrow function
|
|
203
|
+
const results = await pool.map([1, 2, 3], n => n * 2);
|
|
204
|
+
|
|
205
|
+
// Regular function
|
|
206
|
+
const results2 = await pool.map([1, 2, 3], function(n) { return n * 2; });
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### `terminate(): Promise<void>`
|
|
210
|
+
Stops all threads and releases resources.
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
await pool.terminate();
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Thread
|
|
217
|
+
|
|
218
|
+
#### `constructor<T, TArgs>(fn: (...args: TArgs) => T, args?: TArgs)`
|
|
219
|
+
Creates a new thread to execute a function.
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
const thread = new Thread((x: number) => x * x, [5]);
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### `join(): Promise<T>`
|
|
226
|
+
Waits for execution to complete and returns the result. Automatically terminates the thread.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
const result = await thread.join();
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Important Notes
|
|
233
|
+
|
|
234
|
+
- Functions execute in an isolated context (separate Worker Thread)
|
|
235
|
+
- Arguments and results must be serializable
|
|
236
|
+
- Closures don't work - functions have no access to external variables
|
|
237
|
+
- Regular and arrow functions are supported
|
|
238
|
+
- `require()` is available inside functions for using Node.js modules
|
|
239
|
+
- Best suited for CPU-intensive tasks (calculations, data processing)
|
|
240
|
+
- For I/O operations (reading files, network) use async/await instead of threads
|
|
241
|
+
|
|
242
|
+
## When to Use
|
|
243
|
+
|
|
244
|
+
**Use stardust-parallel-js when:**
|
|
245
|
+
- Processing large data arrays
|
|
246
|
+
- Performing complex calculations
|
|
247
|
+
- Parsing or transforming data
|
|
248
|
+
- Processing images/video
|
|
249
|
+
- Need to utilize all CPU cores
|
|
250
|
+
|
|
251
|
+
**Don't use when:**
|
|
252
|
+
- Simple operations (faster to execute sequentially)
|
|
253
|
+
- I/O operations (files, network, DB) - they're already asynchronous
|
|
254
|
+
- Working with DOM (Node.js only)
|
|
255
|
+
|
|
256
|
+
## Choosing Pool Size
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import os from 'os';
|
|
260
|
+
|
|
261
|
+
// Optimal: number of CPU cores
|
|
262
|
+
const pool = new ThreadPool(os.cpus().length);
|
|
263
|
+
|
|
264
|
+
// For CPU-intensive tasks
|
|
265
|
+
const pool = new ThreadPool(os.cpus().length - 1); // leave 1 core for system
|
|
266
|
+
|
|
267
|
+
// For mixed workload
|
|
268
|
+
const pool = new ThreadPool(os.cpus().length * 2);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Comparison with Alternatives
|
|
272
|
+
|
|
273
|
+
| Solution | Simplicity | Performance | TypeScript | Size |
|
|
274
|
+
|----------|-----------|-------------|------------|------|
|
|
275
|
+
| **stardust-parallel-js** | High | High | Full | 9.3kB |
|
|
276
|
+
| worker_threads | Medium | High | Partial | Built-in |
|
|
277
|
+
| cluster | Medium | Medium | Partial | Built-in |
|
|
278
|
+
| child_process | Low | Low | No | Built-in |
|
|
279
|
+
|
|
280
|
+
## Roadmap
|
|
281
|
+
|
|
282
|
+
- [x] Support for transferable objects for large data
|
|
283
|
+
- [ ] Automatic selection of optimal pool size
|
|
284
|
+
- [ ] Task prioritization
|
|
285
|
+
- [ ] Monitoring and statistics
|
|
286
|
+
- [ ] Support for async functions in threads
|
|
287
|
+
|
|
288
|
+
## Feedback
|
|
289
|
+
|
|
290
|
+
Found a bug or have an idea? [Create an issue](https://github.com/b1411/parallel.js).
|
|
291
|
+
|
|
292
|
+
## Requirements
|
|
293
|
+
|
|
294
|
+
- Node.js >= 14.0.0 (with Worker Threads support)
|
|
295
|
+
|
|
296
|
+
## License
|
|
297
|
+
|
|
298
|
+
MIT Β© [b1411](https://github.com/b1411)
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
<p align="center">
|
|
303
|
+
Made with β€οΈ for the Node.js community
|
|
304
|
+
</p>
|
|
305
|
+
|
|
306
|
+
|