Skip to content

Commit 786d167

Browse files
perf: pre-size the map result array
Growing the array, or `Array.from({ length })`, costs measurably more on the small collections `map` and `parallel` are called with.
1 parent dcf49e3 commit 786d167

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎lib/util/async.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ const map = (collection, iterator, callback) => {
153153
const array = Array.isArray(collection)
154154
? /** @type {T[]} */ (collection)
155155
: undefined;
156+
// Pre-sized, because growing it or `Array.from` costs measurably more on
157+
// the small collections this is called with.
156158
/** @type {R[]} */
157-
const results = [];
158-
// Sized up front rather than grown, so a result never lands past the end
159-
// while earlier items are still running.
160-
if (array !== undefined) results.length = array.length;
159+
// eslint-disable-next-line unicorn/no-new-array
160+
const results = array === undefined ? [] : new Array(array.length);
161161
let remaining = 1;
162162
let finished = false;
163163

0 commit comments

Comments
 (0)