perf: ArraySet.add returns index — drop trailing indexOf in hot paths#65
Merged
Conversation
Three per-mapping hot paths followed `add()` with `indexOf()` to grab the inserted/existing index. Both call into the same Map: `add` does a has+set+get-equivalent, `indexOf` does a redundant get. Returning the index from `add` lets callers get the result for free — one Map op per source/name instead of two. Affected: * SourceMapGenerator.addMapping * SourceMapGenerator.applySourceMap * IndexedSourceMapConsumer._parseMappings Bench (SOLO=1 PHASES=adding generate vs main, two-process): addMapping mean +21.4% across jridgewell fixtures (preact +30.3%, react +21.4%, amp +20.5%, issue-41 +34.8%, vscode +21.1%, babel.min +0.2% — flat, very few unique sources) API: ArraySet.add gains a return value. Existing callers ignoring it keep working unchanged.
This was referenced May 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ArraySet.addnow returns the string's index in the set (existing or new). Three per-mapping hot paths previously pairedadd()withindexOf()to grab that index — that's two Map operations where one suffices. Lettingaddreturn it cuts the redundantMap.get.Affected hot paths:
SourceMapGenerator.addMapping— once per added mapping (source + name)SourceMapGenerator.applySourceMap— once per rebuilt mapping (source + name)IndexedSourceMapConsumer._parseMappings— once per section mapping (source + name)Bench
SOLO=1 PHASES=adding scripts/bench-diff.sh main generate(two-process, ops/sec via benchmark.js):Sample sizes 24–100 runs per cell, error bars ≤3%. Mean +21.4%. babel.min is flat because it has very few unique sources/names — the saved
Map.getis amortized away.API
ArraySet.addgains a return value (the index). Existing callers that ignore the return value continue to work unchanged — no breaking change.Test plan