Skip to content

Commit 294b868

Browse files
nightknightokgrytePlaneshifterstdlib-bot
authored
feat: add utils/async/parallel
PR-URL: #1896 Closes: #1811 Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Athan <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent ca56638 commit 294b868

File tree

17 files changed

+2867
-0
lines changed

17 files changed

+2867
-0
lines changed
+266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# parallel
22+
23+
> Execute functions in parallel and pass the results of all functions to a provided callback.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var parallel = require( '@stdlib/utils/async/parallel' );
41+
```
42+
43+
#### parallel( fcns, \[options,] done )
44+
45+
Executes a set of functions in parallel and passes the results of all functions to a provided callback.
46+
47+
```javascript
48+
function a( clbk ) {
49+
setTimeout( onTimeout, 0 );
50+
function onTimeout() {
51+
clbk( null, 2 );
52+
}
53+
}
54+
55+
function b( clbk ) {
56+
setTimeout( onTimeout, 0 );
57+
function onTimeout() {
58+
clbk( null, 4 );
59+
}
60+
}
61+
62+
function done( error, out ) {
63+
if ( error ) {
64+
throw error;
65+
}
66+
console.log( out );
67+
// => [ 2, 4 ]
68+
}
69+
70+
var fcns = [ a, b ];
71+
72+
parallel( fcns, done );
73+
```
74+
75+
The function accepts the following `options`:
76+
77+
- **limit**: maximum number of functions to execute concurrently. Default: `infinity`.
78+
- **thisArg**: execution context for each function.
79+
80+
To limit the maximum number of functions executing in parallel, set the `limit` option.
81+
82+
```javascript
83+
function a( clbk ) {
84+
setTimeout( onTimeout, 0 );
85+
function onTimeout() {
86+
clbk( null, 2 );
87+
}
88+
}
89+
90+
function b( clbk ) {
91+
setTimeout( onTimeout, 0 );
92+
function onTimeout() {
93+
clbk( null, 4 );
94+
}
95+
}
96+
97+
function done( error, out ) {
98+
if ( error ) {
99+
throw error;
100+
}
101+
console.log( out );
102+
// => [ 2, 4 ]
103+
}
104+
105+
var fcns = [ a, b ];
106+
107+
var opts = {
108+
'limit': 1
109+
};
110+
111+
parallel( fcns, opts, done );
112+
```
113+
114+
To set the `this` context for **all** `functions` in the provided function array, set the `thisArg` option.
115+
116+
```javascript
117+
function a( clbk ) {
118+
this.idx += 1;
119+
clbk( null, 2 );
120+
}
121+
122+
function b( clbk ) {
123+
this.idx += 1;
124+
clbk( null, 4 );
125+
}
126+
127+
var fcns = [ a, b ];
128+
var ctx = {
129+
'idx': 0
130+
};
131+
var opts = {
132+
'thisArg': ctx
133+
};
134+
135+
parallel( fcns, opts, done );
136+
137+
function done( error, out ) {
138+
if ( error ) {
139+
throw error;
140+
}
141+
console.log( ctx.idx );
142+
// => 2
143+
}
144+
```
145+
146+
#### parallel.factory( fcns, \[options] )
147+
148+
Returns a reusable function which executes a set of functions in parallel.
149+
150+
```javascript
151+
function a( clbk ) {
152+
setTimeout( onTimeout, 0 );
153+
function onTimeout() {
154+
clbk( null, 2 );
155+
}
156+
}
157+
158+
function b( clbk ) {
159+
setTimeout( onTimeout, 0 );
160+
function onTimeout() {
161+
clbk( null, 4 );
162+
}
163+
}
164+
165+
function done( error, out ) {
166+
if ( error ) {
167+
throw error;
168+
}
169+
console.log( out );
170+
// => [ 2, 4 ]
171+
}
172+
173+
var fcns = [ a, b ];
174+
175+
var run = parallel.factory( fcns );
176+
177+
run( done );
178+
run( done );
179+
run( done );
180+
```
181+
182+
</section>
183+
184+
<!-- /.usage -->
185+
186+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
187+
188+
<section class="notes">
189+
190+
## Notes
191+
192+
- The order of the results provided to the `done` callback corresponds to the order of the provided functions.
193+
- When executed, each provided function is invoked with a single callback argument. The callback should be invoked upon function completion. The first argument is reserved as an `error` argument (which can be `null`). The second argument is reserved for any results which should be passed to the `done` callback upon completion of all provided functions.
194+
- If any function fails to invoke the callback argument, the `done` callback will never be invoked.
195+
- This implementation is intended to start asynchronous tasks so that execution of each task runs concurrently. If provided a function which does not perform asynchronous tasks, the function will execute synchronously. Hence, this implementation does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a `function` which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
196+
- The function executes provided functions in the same thread. Accordingly, the function does **not** spawn new threads.
197+
198+
</section>
199+
200+
<!-- /.notes -->
201+
202+
<!-- Package usage examples. -->
203+
204+
<section class="examples">
205+
206+
## Examples
207+
208+
<!-- eslint no-undef: "error" -->
209+
210+
```javascript
211+
var parallel = require( '@stdlib/utils/async/parallel' );
212+
213+
function foo( clbk ) {
214+
setTimeout( onTimeout, 300 );
215+
function onTimeout() {
216+
clbk( null, 'one' );
217+
}
218+
}
219+
220+
function bar( clbk ) {
221+
setTimeout( onTimeout, 100 );
222+
function onTimeout() {
223+
clbk( null, 'two' );
224+
}
225+
}
226+
227+
function done( error, results ) {
228+
if ( error ) {
229+
throw error;
230+
}
231+
console.log( results );
232+
// => [ 'one', 'two' ]
233+
}
234+
235+
var fcns = [ foo, bar ];
236+
237+
parallel( fcns, done );
238+
```
239+
240+
</section>
241+
242+
<!-- /.examples -->
243+
244+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
245+
246+
<section class="references">
247+
248+
</section>
249+
250+
<!-- /.references -->
251+
252+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
253+
254+
<section class="related">
255+
256+
</section>
257+
258+
<!-- /.related -->
259+
260+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
261+
262+
<section class="links">
263+
264+
</section>
265+
266+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pkg = require( './../package.json' ).name;
25+
var factory = require( './../lib/factory.js' );
26+
27+
28+
// FUNCTIONS //
29+
30+
/**
31+
* Returns a function which runs asynchronously.
32+
*
33+
* @private
34+
* @param {*} value - return value
35+
* @returns {Function} function which runs asynchronously
36+
*/
37+
function fcn( value ) {
38+
return f;
39+
40+
function f( clbk ) {
41+
setTimeout( onTimeout, 0 );
42+
43+
function onTimeout() {
44+
clbk( null, value );
45+
}
46+
}
47+
}
48+
49+
50+
// MAIN //
51+
52+
bench( pkg+':factory', function benchmark( b ) {
53+
var parallel;
54+
var arr;
55+
var i;
56+
57+
arr = [
58+
fcn( 'one' ),
59+
fcn( 'two' ),
60+
fcn( 'three' ),
61+
fcn( 'four' )
62+
];
63+
64+
parallel = factory( arr );
65+
66+
i = 0;
67+
b.tic();
68+
69+
return next();
70+
71+
function next( error ) {
72+
i += 1;
73+
if ( error ) {
74+
b.fail( 'should not return an error' );
75+
}
76+
if ( i <= b.iterations ) {
77+
return parallel( next );
78+
}
79+
b.toc();
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
}
83+
});

0 commit comments

Comments
 (0)