|
4 | 4 |
|
5 | 5 | > Stability: 1 - Experimental
|
6 | 6 |
|
7 |
| -The `worker` module provides a way to create multiple environments running |
8 |
| -on independent threads, and to create message channels between them. It |
9 |
| -can be accessed using: |
| 7 | +The `worker_threads` module enables the use of threads with message channels |
| 8 | +between them. To access it: |
10 | 9 |
|
11 | 10 | ```js
|
12 | 11 | const worker = require('worker_threads');
|
13 | 12 | ```
|
14 | 13 |
|
15 |
| -Workers are useful for performing CPU-intensive JavaScript operations; do not |
16 |
| -use them for I/O, since Node.js’s built-in mechanisms for performing operations |
17 |
| -asynchronously already treat it more efficiently than Worker threads can. |
| 14 | +Workers (threads) are useful for performing CPU-intensive JavaScript operations. |
| 15 | +They will not help much with I/O-intensive work. Node.js’s built-in asynchronous |
| 16 | +I/O operations are more efficient than Workers can be. |
18 | 17 |
|
19 |
| -Workers, unlike child processes or when using the `cluster` module, can also |
20 |
| -share memory efficiently by transferring `ArrayBuffer` instances or sharing |
21 |
| -`SharedArrayBuffer` instances between them. |
| 18 | +Unlike `child_process` or `cluster`, `worker_threads` can share memory. They do |
| 19 | +so by transferring `ArrayBuffer` instances or sharing `SharedArrayBuffer` |
| 20 | +instances. |
22 | 21 |
|
23 | 22 | ```js
|
24 | 23 | const {
|
@@ -46,10 +45,9 @@ if (isMainThread) {
|
46 | 45 | }
|
47 | 46 | ```
|
48 | 47 |
|
49 |
| -Note that this example spawns a Worker thread for each `parse` call. |
50 |
| -In practice, it is strongly recommended to use a pool of Workers for these |
51 |
| -kinds of tasks, since the overhead of creating Workers would likely exceed the |
52 |
| -benefit of handing the work off to it. |
| 48 | +The above example spawns a Worker thread for each `parse()` call. In actual |
| 49 | +practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the |
| 50 | +overhead of creating Workers would likely exceed their benefit. |
53 | 51 |
|
54 | 52 | ## worker.isMainThread
|
55 | 53 | <!-- YAML
|
|
0 commit comments