You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> **Update: September 2016**: New forces are put in place to drive the code forward.
13
+
**Update: September 2016**
14
+
15
+
> New forces are put in place to drive the code forward.
16
+
17
+
---
18
+
19
+
Parallel.js is a library for to make parallel computing in Javascript simple. It works in Node.js and in the Web Browser.
14
20
15
-
Parallel.js is a library making JavScript parallel computing simple.
16
-
Parallel.js works in Node.js and in the web browser for client side, server side or a mixture of client and server side compute.
17
-
Parallel.js takes advantage of Web Workers for the web, and child processes for Node.
21
+
Parallel takes advantage of Web Workers for the web, and child processes for Node.
18
22
19
-
# Installation
23
+
---
24
+
25
+
## Installation
20
26
21
27
You can download the raw javascript file [here](https://raw.github.com/adambom/parallel.js/master/lib/parallel.js)
22
28
23
29
Just include it via a script tag in your HTML page
24
30
25
-
Parallel.js is also available as a node module:
31
+
**Parallel.js is also available as a node module**
26
32
27
33
```bash
28
34
npm install paralleljs --save
29
35
```
30
36
31
-
# Usage
37
+
##Usage
32
38
33
-
####`Parallel(data, opts)`
39
+
### `Parallel(data, opts)`
34
40
35
41
This is the constructor. Use it to new up any parallel jobs. The constructor takes an array of data you want to
36
42
operate on. This data will be held in memory until you finish your job, and can be accessed via the `.data` attribute
@@ -39,32 +45,36 @@ of your job.
39
45
The object returned by the `Parallel` constructor is meant to be chained, so you can produce a chain of
40
46
operations on the provided data.
41
47
42
-
*Arguments*
48
+
**Arguments**
49
+
43
50
*`data`: This is the data you wish to operate on. Will often be an array, but the only restrictions are that your values are serializable as JSON.
44
51
*`options` (optional): Some options for your job
45
-
*`evalPath` (optional): This is the path to the file eval.js. This is required when running in node, and required for some browsers (IE 10) in order to work around cross-domain restrictions for web workers. Defaults to the same location as parallel.js in node environments, and `null` in the browser.
46
-
*`maxWorkers` (optional): The maximum number of permitted worker threads. This will default to 4, or the number of cpus on your computer if you're running node
47
-
*`synchronous` (optional): If webworkers are not available, whether or not to fall back to synchronous processing using `setTimeout`. Defaults to `true`.
52
+
*`evalPath` (optional): This is the path to the file eval.js. This is required when running in node, and required for some browsers (IE 10) in order to work around cross-domain restrictions for web workers. Defaults to the same location as parallel.js in node environments, and `null` in the browser.
53
+
*`maxWorkers` (optional): The maximum number of permitted worker threads. This will default to 4, or the number of cpus on your computer if you're running node
54
+
*`synchronous` (optional): If webworkers are not available, whether or not to fall back to synchronous processing using `setTimeout`. Defaults to `true`.
48
55
49
-
*Example*
56
+
**Example**
50
57
51
58
```js
52
59
constp=newParallel([1, 2, 3, 4, 5]);
53
60
54
61
console.log(p.data); // prints [1, 2, 3, 4, 5]
55
62
```
56
63
57
-
*******
64
+
---
65
+
66
+
### `spawn(fn)`
58
67
59
-
#### `spawn(fn)`
60
68
This function will spawn a new process on a worker thread. Pass it the function you want to call. Your
61
69
function will receive one argument, which is the current data. The value returned from your spawned function will
62
70
update the current data.
63
71
64
-
*Arguments*
72
+
**Arguments**
73
+
65
74
*`fn`: A function to execute on a worker thread. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data.
66
75
67
-
*Example*
76
+
**Example**
77
+
68
78
```js
69
79
constp=newParallel('forwards');
70
80
@@ -81,17 +91,20 @@ p.spawn(data => {
81
91
});
82
92
```
83
93
84
-
*******
94
+
---
95
+
96
+
### `map(fn)`
85
97
86
-
#### `map(fn)`
87
98
Map will apply the supplied function to every element in the wrapped data. Parallel will spawn one worker for
88
99
each array element in the data, or the supplied maxWorkers argument. The values returned will be stored for
89
100
further processing.
90
101
91
-
*Arguments*
102
+
**Arguments**
103
+
92
104
*`fn`: A function to apply. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data.
93
105
94
-
*Example*
106
+
**Example**
107
+
95
108
```js
96
109
constp=newParallel([0, 1, 2, 3, 4, 5, 6]);
97
110
constlog=function () { console.log(arguments); };
@@ -108,17 +121,20 @@ p.map(fib).then(log)
108
121
// Logs the first 7 Fibonnaci numbers, woot!
109
122
```
110
123
111
-
*******
124
+
---
125
+
126
+
### `reduce(fn)`
112
127
113
-
#### `reduce(fn)`
114
128
Reduce applies an operation to every member of the wrapped data, and returns a scalar value produced by the operation.
115
129
Use it for combining the results of a map operation, by summing numbers for example. This takes a reducing function,
116
130
which gets an argument, `data`, an array of the stored value, and the current element.
117
131
118
-
*Arguments*
132
+
**Arguments**
133
+
119
134
*`fn`: A function to apply. Receives the stored value and current element as argument. The value returned will be stored as the current value for the next iteration. Finally, the current value will be assigned to current data.
120
135
121
-
*Example*
136
+
**Example**
137
+
122
138
```js
123
139
constp=newParallel([0, 1, 2, 3, 4, 5, 6, 7, 8]);
124
140
@@ -132,17 +148,20 @@ p.require(factorial)
132
148
p.map((n=>Math.pow(10, n)).reduce(add).then(log);
133
149
```
134
150
135
-
*******
151
+
---
152
+
153
+
### `then(success, fail)`
136
154
137
-
#### `then(success, fail)`
138
155
The functions given to `then` are called after the last requested operation has finished.
139
156
`success` receives the resulting data object, while `fail` will receive an error object.
140
157
141
-
*Arguments*
158
+
**Arguments**
159
+
142
160
- `success`: A function that gets called upon successful completion. Receives the wrapped data as an argument.
143
161
- `failure` (optional): A function that gets called if the job fails. The function is passed an error object.
144
162
145
-
*Example*
163
+
**Example**
164
+
146
165
```js
147
166
constp=newParallel([1, 2, 3]);
148
167
@@ -163,15 +182,17 @@ p
163
182
.then(log);
164
183
```
165
184
166
-
*******
185
+
---
186
+
187
+
### `require(state)`
167
188
168
-
#### `require(state)`
169
189
If you have state that you want to share between your main thread and the worker threads, this is how. Require
170
190
takes either a string or a function. A string should point to a file name. Note that in order to
171
191
use ```require``` with a file name as an argument, you have to provide the evalPath property in the options
172
192
object.
173
193
174
-
*Example*
194
+
**Example**
195
+
175
196
```js
176
197
let p =newParallel([1, 2, 3], { evalPath:'https://raw.github.com/adambom/parallel.js/master/lib/eval.js' });
177
198
@@ -185,15 +206,20 @@ p.require('blargh.js');
185
206
186
207
p.map(d=>blargh(20*cubeRoot(d)));
187
208
```
188
-
#### Passing environement to functions
209
+
210
+
---
211
+
212
+
## Passing environement to functions
213
+
189
214
You can pass data to threads that will be global to that worker. This data will be global in each called function.
190
215
The data will be available under the `global.env` namespace. The namespace can be configured by passing the
191
216
`envNamespace` option to the `Parallel` constructor. The data you wish to pass should be provided as the `env` option
192
217
to the parallel constructor.
193
218
194
219
Important: Globals can not be mutated between threads.
195
220
196
-
*Example*
221
+
**Example**
222
+
197
223
```js
198
224
let p =newParallel([1, 2, 3], {
199
225
env: {
@@ -215,5 +241,25 @@ p = new Parallel([1, 2, 3], {
0 commit comments