Skip to content

Commit b3679d5

Browse files
authored
Merge pull request #1395 from caolan/waterfall-optimization
Optimized waterfall, parallel, et al.
2 parents c08f878 + 6ef3a91 commit b3679d5

26 files changed

+281
-265
lines changed

dist/async.js

+108-148
Large diffs are not rendered by default.

dist/async.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/async.min.map

+1-1
Large diffs are not rendered by default.

lib/apply.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import rest from './internal/rest';
1+
import slice from './internal/slice';
22

33
/**
44
* Creates a continuation function with some arguments already applied.
@@ -12,10 +12,11 @@ import rest from './internal/rest';
1212
* @memberOf module:Utils
1313
* @method
1414
* @category Util
15-
* @param {Function} function - The function you want to eventually apply all
15+
* @param {Function} fn - The function you want to eventually apply all
1616
* arguments to. Invokes with (arguments...).
1717
* @param {...*} arguments... - Any number of arguments to automatically apply
1818
* when the continuation is called.
19+
* @returns {Function} the partially-applied function
1920
* @example
2021
*
2122
* // using apply
@@ -44,8 +45,10 @@ import rest from './internal/rest';
4445
* two
4546
* three
4647
*/
47-
export default rest(function(fn, args) {
48-
return rest(function(callArgs) {
48+
export default function(fn/*, ...args*/) {
49+
var args = slice(arguments, 1);
50+
return function(/*callArgs*/) {
51+
var callArgs = slice(arguments);
4952
return fn.apply(null, args.concat(callArgs));
50-
});
51-
});
53+
};
54+
};

lib/auto.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import indexOf from 'lodash/_baseIndexOf';
44
import isArray from 'lodash/isArray';
55
import okeys from 'lodash/keys';
66
import noop from 'lodash/noop';
7-
import rest from './internal/rest';
87

8+
import slice from './internal/slice';
99
import once from './internal/once';
1010
import onlyOnce from './internal/onlyOnce';
1111
import wrapAsync from './internal/wrapAsync';
@@ -192,26 +192,26 @@ export default function (tasks, concurrency, callback) {
192192
function runTask(key, task) {
193193
if (hasError) return;
194194

195-
var taskCallback = onlyOnce(rest(function(err, args) {
195+
var taskCallback = onlyOnce(function(err, result) {
196196
runningTasks--;
197-
if (args.length <= 1) {
198-
args = args[0];
197+
if (arguments.length > 2) {
198+
result = slice(arguments, 1);
199199
}
200200
if (err) {
201201
var safeResults = {};
202202
forOwn(results, function(val, rkey) {
203203
safeResults[rkey] = val;
204204
});
205-
safeResults[key] = args;
205+
safeResults[key] = result;
206206
hasError = true;
207207
listeners = Object.create(null);
208208

209209
callback(err, safeResults);
210210
} else {
211-
results[key] = args;
211+
results[key] = result;
212212
taskComplete(key);
213213
}
214-
}));
214+
});
215215

216216
runningTasks++;
217217
var taskFn = wrapAsync(task[task.length - 1]);

lib/compose.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import seq from './seq';
2-
import rest from './internal/rest';
2+
import slice from './internal/slice';
33

44
/**
55
* Creates a function which is a composition of the passed asynchronous
@@ -36,6 +36,6 @@ import rest from './internal/rest';
3636
* // result now equals 15
3737
* });
3838
*/
39-
export default rest(function(args) {
40-
return seq.apply(null, args.reverse());
41-
});
39+
export default function(/*...args*/) {
40+
return seq.apply(null, slice(arguments).reverse());
41+
};

lib/constant.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import rest from './internal/rest';
2-
import initialParams from './internal/initialParams';
1+
import slice from './internal/slice';
32

43
/**
54
* Returns a function that when called, calls-back with the values provided.
@@ -43,9 +42,11 @@ import initialParams from './internal/initialParams';
4342
* //...
4443
* }, callback);
4544
*/
46-
export default rest(function(values) {
45+
export default function(/*...values*/) {
46+
var values = slice(arguments);
4747
var args = [null].concat(values);
48-
return initialParams(function (ignoredArgs, callback) {
48+
return function (/*...ignoredArgs, callback*/) {
49+
var callback = arguments[arguments.length - 1];
4950
return callback.apply(this, args);
50-
});
51-
});
51+
};
52+
};

lib/doDuring.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import noop from 'lodash/noop';
2-
import rest from './internal/rest';
2+
import slice from './internal/slice';
33
import onlyOnce from './internal/onlyOnce';
44
import wrapAsync from './internal/wrapAsync';
55

@@ -28,11 +28,12 @@ export default function doDuring(fn, test, callback) {
2828
var _fn = wrapAsync(fn);
2929
var _test = wrapAsync(test);
3030

31-
var next = rest(function(err, args) {
32-
if (err) return callback(err);
31+
function next(err/*, ...args*/) {
32+
if (err) return callback(err);
33+
var args = slice(arguments, 1);
3334
args.push(check);
3435
_test.apply(this, args);
35-
});
36+
};
3637

3738
function check(err, truth) {
3839
if (err) return callback(err);

lib/doWhilst.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import noop from 'lodash/noop';
2-
import rest from './internal/rest';
2+
import slice from './internal/slice';
33

44
import onlyOnce from './internal/onlyOnce';
55
import wrapAsync from './internal/wrapAsync';
@@ -29,10 +29,11 @@ import wrapAsync from './internal/wrapAsync';
2929
export default function doWhilst(iteratee, test, callback) {
3030
callback = onlyOnce(callback || noop);
3131
var _iteratee = wrapAsync(iteratee);
32-
var next = rest(function(err, args) {
32+
var next = function(err/*, ...args*/) {
3333
if (err) return callback(err);
34+
var args = slice(arguments, 1);
3435
if (test.apply(this, args)) return _iteratee(next);
3536
callback.apply(null, [null].concat(args));
36-
});
37+
};
3738
_iteratee(next);
3839
}

lib/internal/applyEach.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import rest from './rest';
1+
import slice from './slice';
22
import initialParams from './initialParams';
33
import wrapAsync from './wrapAsync';
44

55
export default function applyEach(eachfn) {
6-
return rest(function(fns, args) {
6+
return function(fns/*, ...args*/) {
7+
var args = slice(arguments, 1);
78
var go = initialParams(function(args, callback) {
89
var that = this;
910
return eachfn(fns, function (fn, cb) {
@@ -16,5 +17,5 @@ export default function applyEach(eachfn) {
1617
else {
1718
return go;
1819
}
19-
});
20+
};
2021
}

lib/internal/consoleFunc.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import arrayEach from 'lodash/_arrayEach';
2-
import rest from './rest';
2+
import slice from './slice';
33
import wrapAsync from './wrapAsync';
44

55
export default function consoleFunc(name) {
6-
return rest(function (fn, args) {
7-
wrapAsync(fn).apply(null, args.concat(rest(function (err, args) {
6+
return function (fn/*, ...args*/) {
7+
var args = slice(arguments, 1);
8+
args.push(function (err/*, ...args*/) {
9+
var args = slice(arguments, 1);
810
if (typeof console === 'object') {
911
if (err) {
1012
if (console.error) {
1113
console.error(err);
1214
}
13-
}
14-
else if (console[name]) {
15+
} else if (console[name]) {
1516
arrayEach(args, function (x) {
1617
console[name](x);
1718
});
1819
}
1920
}
20-
})));
21-
});
21+
})
22+
wrapAsync(fn).apply(null, args);
23+
};
2224
}

lib/internal/initialParams.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import rest from './rest';
1+
import slice from './slice';
22

33
export default function (fn) {
4-
return rest(function (args/*..., callback*/) {
4+
return function (/*...args, callback*/) {
5+
var args = slice(arguments);
56
var callback = args.pop();
67
fn.call(this, args, callback);
7-
});
8+
};
89
}

lib/internal/parallel.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import noop from 'lodash/noop';
22
import isArrayLike from 'lodash/isArrayLike';
3-
import rest from './rest';
3+
import slice from './slice';
44
import wrapAsync from './wrapAsync';
55

66
export default function _parallel(eachfn, tasks, callback) {
77
callback = callback || noop;
88
var results = isArrayLike(tasks) ? [] : {};
99

1010
eachfn(tasks, function (task, key, callback) {
11-
wrapAsync(task)(rest(function (err, args) {
12-
if (args.length <= 1) {
13-
args = args[0];
11+
wrapAsync(task)(function (err, result) {
12+
if (arguments.length > 2) {
13+
result = slice(arguments, 1);
1414
}
15-
results[key] = args;
15+
results[key] = result;
1616
callback(err);
17-
}));
17+
});
1818
}, function (err) {
1919
callback(err, results);
2020
});

lib/internal/queue.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import indexOf from 'lodash/_baseIndexOf';
22
import isArray from 'lodash/isArray';
33
import noop from 'lodash/noop';
4-
import rest from './rest';
54

65
import onlyOnce from './onlyOnce';
76
import setImmediate from './setImmediate';
@@ -51,7 +50,7 @@ export default function queue(worker, concurrency, payload) {
5150
}
5251

5352
function _next(tasks) {
54-
return rest(function(args){
53+
return function(err){
5554
numRunning -= 1;
5655

5756
for (var i = 0, l = tasks.length; i < l; i++) {
@@ -61,10 +60,10 @@ export default function queue(worker, concurrency, payload) {
6160
workersList.splice(index)
6261
}
6362

64-
task.callback.apply(task, args);
63+
task.callback.apply(task, arguments);
6564

66-
if (args[0] != null) {
67-
q.error(args[0], task.data);
65+
if (err != null) {
66+
q.error(err, task.data);
6867
}
6968
}
7069

@@ -76,7 +75,7 @@ export default function queue(worker, concurrency, payload) {
7675
q.drain();
7776
}
7877
q.process();
79-
});
78+
};
8079
}
8180

8281
var isProcessing = false;

lib/internal/rest.js

-8
This file was deleted.

lib/internal/setImmediate.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import rest from './rest';
3+
import slice from './slice';
44

55
export var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
66
export var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';
@@ -10,11 +10,12 @@ export function fallback(fn) {
1010
}
1111

1212
export function wrap(defer) {
13-
return rest(function (fn, args) {
13+
return function (fn/*, ...args*/) {
14+
var args = slice(arguments, 1);
1415
defer(function () {
1516
fn.apply(null, args);
1617
});
17-
});
18+
};
1819
}
1920

2021
var _defer;

lib/internal/slice.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function slice(arrayLike, start) {
2+
start = start|0;
3+
var newLen = Math.max(arrayLike.length - start, 0);
4+
var newArr = Array(newLen);
5+
for(var idx = 0; idx < newLen; idx++) {
6+
newArr[idx] = arrayLike[start + idx];
7+
}
8+
return newArr;
9+
}

lib/memoize.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import identity from 'lodash/identity';
2-
import rest from './internal/rest';
2+
import slice from './internal/slice';
33

44
import setImmediate from './internal/setImmediate';
55
import initialParams from './internal/initialParams';
@@ -61,14 +61,15 @@ export default function memoize(fn, hasher) {
6161
queues[key].push(callback);
6262
} else {
6363
queues[key] = [callback];
64-
_fn.apply(null, args.concat(rest(function(args) {
64+
_fn.apply(null, args.concat(function(/*args*/) {
65+
var args = slice(arguments);
6566
memo[key] = args;
6667
var q = queues[key];
6768
delete queues[key];
6869
for (var i = 0, l = q.length; i < l; i++) {
6970
q[i].apply(null, args);
7071
}
71-
})));
72+
}));
7273
}
7374
});
7475
memoized.memo = memo;

lib/reduceRight.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import reduce from './reduce';
2-
3-
var slice = Array.prototype.slice;
2+
import slice from './internal/slice';
43

54
/**
65
* Same as [`reduce`]{@link module:Collections.reduce}, only operates on `array` in reverse order.
@@ -25,6 +24,6 @@ var slice = Array.prototype.slice;
2524
* (err, result).
2625
*/
2726
export default function reduceRight (array, memo, iteratee, callback) {
28-
var reversed = slice.call(array).reverse();
27+
var reversed = slice(array).reverse();
2928
reduce(reversed, memo, iteratee, callback);
3029
}

0 commit comments

Comments
 (0)