From b26b205f1551e4c89651b279aeb54c6e5ae3e86c Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 29 Aug 2016 16:34:42 +0300 Subject: [PATCH] Don't allocate an additional array in Promise.each Closes #1057 --- src/each.js | 9 +++++---- src/map.js | 3 +-- src/reduce.js | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/each.js b/src/each.js index ac8ead77f..e4f3d05ba 100644 --- a/src/each.js +++ b/src/each.js @@ -12,8 +12,8 @@ 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) { @@ -21,9 +21,10 @@ Promise.prototype.mapSeries = function (fn) { }; 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; }; + diff --git a/src/map.js b/src/map.js index 794c3ec22..d2bf918be 100644 --- a/src/map.js +++ b/src/map.js @@ -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); @@ -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); diff --git a/src/reduce.js b/src/reduce.js index d2acf0513..76f31eb07 100644 --- a/src/reduce.js +++ b/src/reduce.js @@ -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; };