Skip to content

perf(concat): avoid repeatedly copying accumulated results - #2132

Open
chaser940428-pixel wants to merge 2 commits into
caolan:masterfrom
chaser940428-pixel:perf/concat-linear-aggregation
Open

chaser940428-pixel wants to merge 2 commits into
caolan:masterfrom
chaser940428-pixel:perf/concat-linear-aggregation

Conversation

@chaser940428-pixel

Copy link
Copy Markdown

Problem and change

concatLimit currently calls result.concat(...mapResults[i]) for every completed item. This copies the entire accumulated prefix each time. When many iteratees each return a small array, aggregation does quadratic copying after the async work is already done.

Append each batch at an offset in the output instead. Keep native [].concat(...args) for each individual batch so variadic results, one-level flattening and Symbol.isConcatSpreadable keep their existing behavior. Extend the output length and copy only present indices, preserving sparse holes without walking their empty ranges. The shared path covers concat, concatLimit and concatSeries.

Add 12 compatibility cases across those three APIs (sparse results, spreadability/nesting, variadic falsy values and Promise results), plus a small standalone perf/concat-aggregation.js benchmark. No timing threshold is added to unit tests. The compatibility cases protect existing behavior; they are not claimed as previously failing correctness tests.

Validation

Windows / Node 24.22.1:

  • npm test: 715 passing, including the new compatibility cases; lint passes.
  • git diff --check: passes.
  • Babel's cache is directed to a workspace-local file via BABEL_CACHE_PATH; the initial default-home cache write was denied by the execution environment. No test assertion or timeout was changed.

The same new benchmark file was run separately against unchanged 3921e54 and this source tree, after warmup, taking the median of five samples per size:

Input items Base Updated
10,000 44.39 ms 5.14 ms
20,000 354.84 ms 8.95 ms
40,000 2009.52 ms 18.49 ms

These are synthetic synchronous one-element results with concurrency 8, intended to isolate aggregation from I/O latency, not end-to-end application speedups. Every sample checks exact output equality outside the timing window. Reproduce with node perf/concat-aggregation.js after installing dependencies; copy the same benchmark file into a base checkout to compare. The unchanged project CI also passes on this head: fork run 43399329709. It covers lint, Node 18/20/21 on Ubuntu, Node 20 on Windows, and FirefoxHeadless browser tests on both operating systems (Node 20).

AI assistance

OpenAI Codex (GPT-6 family) investigated the source, implemented the change, ran tests/benchmarks and drafted this PR under the account owner's authorization. This was found during source inspection, not a claimed production incident. No independent human code review is claimed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant