Skip to content

Commit aa5673e

Browse files
committed
Add a fast path for eachOf
1 parent ddef433 commit aa5673e

File tree

8 files changed

+62
-22
lines changed

8 files changed

+62
-22
lines changed

lib/each.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import eachLimit from './eachLimit';
2-
import doLimit from './internal/doLimit';
1+
import eachOf from './eachOf';
2+
import withoutIndex from './internal/withoutIndex';
33

44
/**
55
* Applies the function `iteratee` to each item in `coll`, in parallel.
@@ -60,4 +60,6 @@ import doLimit from './internal/doLimit';
6060
* }
6161
* });
6262
*/
63-
export default doLimit(eachLimit, Infinity);
63+
export default function eachLimit(coll, iteratee, callback) {
64+
eachOf(coll, withoutIndex(iteratee), callback);
65+
}

lib/eachOf.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1+
import isArrayLike from 'lodash/isArrayLike';
2+
13
import eachOfLimit from './eachOfLimit';
24
import doLimit from './internal/doLimit';
5+
import noop from 'lodash/noop';
6+
import once from 'lodash/once';
7+
import onlyOnce from './internal/onlyOnce';
8+
9+
// eachOf implementation optimized for array-likes
10+
function eachOfArrayLike(coll, iteratee, callback) {
11+
callback = once(callback || noop);
12+
var index = 0,
13+
completed = 0,
14+
length = coll.length;
15+
if (length === 0) {
16+
callback(null);
17+
}
18+
19+
function iteratorCallback(err) {
20+
if (err) {
21+
callback(err);
22+
} else if (++completed === length) {
23+
callback(null);
24+
}
25+
}
26+
27+
for (; index < length; index++) {
28+
iteratee(coll[index], index, onlyOnce(iteratorCallback));
29+
}
30+
}
31+
32+
// a generic version of eachOf which can handle array, object, and iterator cases.
33+
var eachOfGeneric = doLimit(eachOfLimit, Infinity);
334

435
/**
536
* Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
@@ -42,4 +73,7 @@ import doLimit from './internal/doLimit';
4273
* doSomethingWith(configs);
4374
* });
4475
*/
45-
export default doLimit(eachOfLimit, Infinity);
76+
export default function(coll, iteratee, callback) {
77+
var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric;
78+
eachOfImplementation(coll, iteratee, callback);
79+
}

lib/every.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import everyLimit from './everyLimit';
2-
import doLimit from './internal/doLimit';
1+
import createTester from './internal/createTester';
2+
import eachOf from './eachOf';
3+
import notId from './internal/notId';
34

45
/**
56
* Returns `true` if every element in `coll` satisfies an async test. If any
@@ -29,4 +30,4 @@ import doLimit from './internal/doLimit';
2930
* // if result is true then every file exists
3031
* });
3132
*/
32-
export default doLimit(everyLimit, Infinity);
33+
export default createTester(eachOf, notId, notId);

lib/filter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import filterLimit from './filterLimit';
2-
import doLimit from './internal/doLimit';
1+
import filter from './internal/filter';
2+
import doParallel from './internal/doParallel';
33

44
/**
55
* Returns a new array of all the values in `coll` which pass an async truth
@@ -28,4 +28,4 @@ import doLimit from './internal/doLimit';
2828
* // results now equals an array of the existing files
2929
* });
3030
*/
31-
export default doLimit(filterLimit, Infinity);
31+
export default doParallel(filter);

lib/map.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import mapLimit from './mapLimit';
2-
import doLimit from './internal/doLimit';
1+
import doParallel from './internal/doParallel';
2+
import map from './internal/map';
33

44
/**
55
* Produces a new collection of values by mapping each value in `coll` through
@@ -37,4 +37,4 @@ import doLimit from './internal/doLimit';
3737
* // results is now an array of stats for each file
3838
* });
3939
*/
40-
export default doLimit(mapLimit, Infinity);
40+
export default doParallel(map);

lib/parallel.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import parallelLimit from './parallelLimit';
2-
import doLimit from './internal/doLimit';
1+
import eachOf from './eachOf';
2+
import parallel from './internal/parallel';
33

44
/**
55
* Run the `tasks` collection of functions in parallel, without waiting until
@@ -67,4 +67,6 @@ import doLimit from './internal/doLimit';
6767
* // results is now equals to: {one: 1, two: 2}
6868
* });
6969
*/
70-
export default doLimit(parallelLimit, Infinity);
70+
export default function parallelLimit(tasks, callback) {
71+
parallel(eachOf, tasks, callback);
72+
}

lib/reject.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import rejectLimit from './rejectLimit';
2-
import doLimit from './internal/doLimit';
1+
import reject from './internal/reject';
2+
import doParallel from './internal/doParallel';
33

44
/**
55
* The opposite of [`filter`]{@link module:Collections.filter}. Removes values that pass an `async` truth test.
@@ -27,4 +27,4 @@ import doLimit from './internal/doLimit';
2727
* createFiles(results);
2828
* });
2929
*/
30-
export default doLimit(rejectLimit, Infinity);
30+
export default doParallel(reject);

lib/some.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import someLimit from './someLimit';
2-
import doLimit from './internal/doLimit';
1+
import createTester from './internal/createTester';
2+
import eachOf from './eachOf';
3+
import identity from 'lodash/identity';
34

45
/**
56
* Returns `true` if at least one element in the `coll` satisfies an async test.
@@ -31,4 +32,4 @@ import doLimit from './internal/doLimit';
3132
* // if result is true then at least one of the files exists
3233
* });
3334
*/
34-
export default doLimit(someLimit, Infinity);
35+
export default createTester(eachOf, Boolean, identity);

0 commit comments

Comments
 (0)