summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2016-07-20 19:19:38 -0400
committerGraeme Yeates <yeatesgraeme@gmail.com>2016-07-20 19:39:07 -0400
commitaa5673e23d5f84c954c576a91ca84b8e6b1604f1 (patch)
treea72bb49fe6350912be570aaffedba22bcfe657cc
parentddef4334de9a654d9b34a3faed5eaa37b673d414 (diff)
downloadasync-fast-path-eachOf.tar.gz
Add a fast path for eachOffast-path-eachOf
-rw-r--r--lib/each.js8
-rw-r--r--lib/eachOf.js36
-rw-r--r--lib/every.js7
-rw-r--r--lib/filter.js6
-rw-r--r--lib/map.js6
-rw-r--r--lib/parallel.js8
-rw-r--r--lib/reject.js6
-rw-r--r--lib/some.js7
8 files changed, 62 insertions, 22 deletions
diff --git a/lib/each.js b/lib/each.js
index dc8c501..5bf6bd4 100644
--- a/lib/each.js
+++ b/lib/each.js
@@ -1,5 +1,5 @@
-import eachLimit from './eachLimit';
-import doLimit from './internal/doLimit';
+import eachOf from './eachOf';
+import withoutIndex from './internal/withoutIndex';
/**
* Applies the function `iteratee` to each item in `coll`, in parallel.
@@ -60,4 +60,6 @@ import doLimit from './internal/doLimit';
* }
* });
*/
-export default doLimit(eachLimit, Infinity);
+export default function eachLimit(coll, iteratee, callback) {
+ eachOf(coll, withoutIndex(iteratee), callback);
+}
diff --git a/lib/eachOf.js b/lib/eachOf.js
index 4d453c5..43accc7 100644
--- a/lib/eachOf.js
+++ b/lib/eachOf.js
@@ -1,5 +1,36 @@
+import isArrayLike from 'lodash/isArrayLike';
+
import eachOfLimit from './eachOfLimit';
import doLimit from './internal/doLimit';
+import noop from 'lodash/noop';
+import once from 'lodash/once';
+import onlyOnce from './internal/onlyOnce';
+
+// eachOf implementation optimized for array-likes
+function eachOfArrayLike(coll, iteratee, callback) {
+ callback = once(callback || noop);
+ var index = 0,
+ completed = 0,
+ length = coll.length;
+ if (length === 0) {
+ callback(null);
+ }
+
+ function iteratorCallback(err) {
+ if (err) {
+ callback(err);
+ } else if (++completed === length) {
+ callback(null);
+ }
+ }
+
+ for (; index < length; index++) {
+ iteratee(coll[index], index, onlyOnce(iteratorCallback));
+ }
+}
+
+// a generic version of eachOf which can handle array, object, and iterator cases.
+var eachOfGeneric = doLimit(eachOfLimit, Infinity);
/**
* 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';
* doSomethingWith(configs);
* });
*/
-export default doLimit(eachOfLimit, Infinity);
+export default function(coll, iteratee, callback) {
+ var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric;
+ eachOfImplementation(coll, iteratee, callback);
+}
diff --git a/lib/every.js b/lib/every.js
index 8b2ad05..8330718 100644
--- a/lib/every.js
+++ b/lib/every.js
@@ -1,5 +1,6 @@
-import everyLimit from './everyLimit';
-import doLimit from './internal/doLimit';
+import createTester from './internal/createTester';
+import eachOf from './eachOf';
+import notId from './internal/notId';
/**
* Returns `true` if every element in `coll` satisfies an async test. If any
@@ -29,4 +30,4 @@ import doLimit from './internal/doLimit';
* // if result is true then every file exists
* });
*/
-export default doLimit(everyLimit, Infinity);
+export default createTester(eachOf, notId, notId);
diff --git a/lib/filter.js b/lib/filter.js
index e6d3010..4f0cbf1 100644
--- a/lib/filter.js
+++ b/lib/filter.js
@@ -1,5 +1,5 @@
-import filterLimit from './filterLimit';
-import doLimit from './internal/doLimit';
+import filter from './internal/filter';
+import doParallel from './internal/doParallel';
/**
* Returns a new array of all the values in `coll` which pass an async truth
@@ -28,4 +28,4 @@ import doLimit from './internal/doLimit';
* // results now equals an array of the existing files
* });
*/
-export default doLimit(filterLimit, Infinity);
+export default doParallel(filter);
diff --git a/lib/map.js b/lib/map.js
index eecab03..4c0b226 100644
--- a/lib/map.js
+++ b/lib/map.js
@@ -1,5 +1,5 @@
-import mapLimit from './mapLimit';
-import doLimit from './internal/doLimit';
+import doParallel from './internal/doParallel';
+import map from './internal/map';
/**
* Produces a new collection of values by mapping each value in `coll` through
@@ -37,4 +37,4 @@ import doLimit from './internal/doLimit';
* // results is now an array of stats for each file
* });
*/
-export default doLimit(mapLimit, Infinity);
+export default doParallel(map);
diff --git a/lib/parallel.js b/lib/parallel.js
index 67d6180..506f475 100644
--- a/lib/parallel.js
+++ b/lib/parallel.js
@@ -1,5 +1,5 @@
-import parallelLimit from './parallelLimit';
-import doLimit from './internal/doLimit';
+import eachOf from './eachOf';
+import parallel from './internal/parallel';
/**
* Run the `tasks` collection of functions in parallel, without waiting until
@@ -67,4 +67,6 @@ import doLimit from './internal/doLimit';
* // results is now equals to: {one: 1, two: 2}
* });
*/
-export default doLimit(parallelLimit, Infinity);
+export default function parallelLimit(tasks, callback) {
+ parallel(eachOf, tasks, callback);
+}
diff --git a/lib/reject.js b/lib/reject.js
index 8607614..0ddbaf0 100644
--- a/lib/reject.js
+++ b/lib/reject.js
@@ -1,5 +1,5 @@
-import rejectLimit from './rejectLimit';
-import doLimit from './internal/doLimit';
+import reject from './internal/reject';
+import doParallel from './internal/doParallel';
/**
* 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';
* createFiles(results);
* });
*/
-export default doLimit(rejectLimit, Infinity);
+export default doParallel(reject);
diff --git a/lib/some.js b/lib/some.js
index 7b5341c..fd1780f 100644
--- a/lib/some.js
+++ b/lib/some.js
@@ -1,5 +1,6 @@
-import someLimit from './someLimit';
-import doLimit from './internal/doLimit';
+import createTester from './internal/createTester';
+import eachOf from './eachOf';
+import identity from 'lodash/identity';
/**
* Returns `true` if at least one element in the `coll` satisfies an async test.
@@ -31,4 +32,4 @@ import doLimit from './internal/doLimit';
* // if result is true then at least one of the files exists
* });
*/
-export default doLimit(someLimit, Infinity);
+export default createTester(eachOf, Boolean, identity);