Skip to content

Commit 9068ac2

Browse files
committed
make Promise.all(promises) faster in the non-deprecated case
this at the cost of the speed of `Promise.all(…promises)`, which is deprecated. the new version avoids mucking around with `arguments` in the non-deprecated case, avoiding a variety of deoptimisations by runtimes. it delegates to a separate `variadicAll` function in the deprecated case, which does incur the cost of constructing an `arguments` object and slicing it (twice!), but at least that's outside the fast path now.
1 parent 57ae713 commit 9068ac2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,11 @@ Promise.nodeify = function (fn) {
101101
}
102102
}
103103

104-
Promise.all = function () {
105-
var calledWithArray = arguments.length === 1 && Array.isArray(arguments[0])
106-
var args = Array.prototype.slice.call(calledWithArray ? arguments[0] : arguments)
107-
108-
if (!calledWithArray) {
109-
var err = new Error('Promise.all should be called with a single array, calling it with multiple arguments is deprecated')
110-
err.name = 'Warning'
111-
console.warn(err.stack)
112-
}
104+
Promise.all = function (arr) {
105+
if (arguments.length !== 1 || !Array.isArray(arr))
106+
return variadicAll.apply(this, arguments)
107+
108+
var args = Array.prototype.slice.call(arr)
113109

114110
return new Promise(function (resolve, reject) {
115111
if (args.length === 0) return resolve([])
@@ -137,6 +133,14 @@ Promise.all = function () {
137133
})
138134
}
139135

136+
function variadicAll() {
137+
var err = new Error('Promise.all should be called with a single array, calling it with multiple arguments is deprecated')
138+
err.name = 'Warning'
139+
console.warn(err.stack)
140+
141+
return Promise.all(Array.prototype.slice.call(arguments))
142+
}
143+
140144
Promise.reject = function (value) {
141145
return new Promise(function (resolve, reject) {
142146
reject(value);

0 commit comments

Comments
 (0)