Skip to content

Commit

Permalink
Don't allocate an additional array in Promise.each
Browse files Browse the repository at this point in the history
Closes #1057
  • Loading branch information
benjamingr committed Aug 29, 2016
1 parent 5a381ab commit b26b205
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ function PromiseMapSeries(promises, fn) {
}

Promise.prototype.each = function (fn) {
return this.mapSeries(fn)
._then(promiseAllThis, undefined, undefined, this, undefined);
return PromiseReduce(this, fn, INTERNAL, 0)
._then(promiseAllThis, undefined, undefined, this, undefined);
};

Promise.prototype.mapSeries = function (fn) {
return PromiseReduce(this, fn, INTERNAL, INTERNAL);
};

Promise.each = function (promises, fn) {
return PromiseMapSeries(promises, fn)
._then(promiseAllThis, undefined, undefined, promises, undefined);
return PromiseReduce(promises, fn, INTERNAL, 0)
._then(promiseAllThis, undefined, undefined, promises, undefined);
};

Promise.mapSeries = PromiseMapSeries;
};

3 changes: 1 addition & 2 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var getDomain = Promise._getDomain;
var util = require("./util");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
var EMPTY_ARRAY = [];

function MappingPromiseArray(promises, fn, limit, _filter) {
this.constructor$(promises);
Expand All @@ -22,7 +21,7 @@ function MappingPromiseArray(promises, fn, limit, _filter) {
: null;
this._limit = limit;
this._inFlight = 0;
this._queue = limit >= 1 ? [] : EMPTY_ARRAY;
this._queue = [];
this._init$(undefined, RESOLVE_ARRAY);
}
util.inherits(MappingPromiseArray, PromiseArray);
Expand Down
16 changes: 13 additions & 3 deletions src/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@ function ReductionPromiseArray(promises, fn, initialValue, _each) {
}
this._initialValue = initialValue;
this._currentCancellable = null;
this._eachValues = _each === INTERNAL ? [] : undefined;
if(_each === INTERNAL) {
this._eachValues = Array(this._length);
} else if (_each === 0) {
this._eachValues = null;
} else {
this._eachValues = undefined;
}
this._promise._captureStackTrace();
this._init$(undefined, RESOLVE_CALL_METHOD);
}
util.inherits(ReductionPromiseArray, PromiseArray);

ReductionPromiseArray.prototype._gotAccum = function(accum) {
if (this._eachValues !== undefined && accum !== INTERNAL) {
if (this._eachValues !== undefined &&
this._eachValues !== undefined &&
accum !== INTERNAL) {
this._eachValues.push(accum);
}
};

ReductionPromiseArray.prototype._eachComplete = function(value) {
this._eachValues.push(value);
if (this._eachValues !== null) {
this._eachValues.push(value);
}
return this._eachValues;
};

Expand Down

0 comments on commit b26b205

Please sign in to comment.