From d4d26495c1886c6a0f13dd16934eef431d604d0e Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Sun, 10 May 2026 23:29:15 +0400 Subject: [PATCH] perf: inline optional getArg in BasicSourceMapConsumer constructor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four sites — names, sourceRoot, sourcesContent, file — all optional with documented defaults. Same pattern as #59 / #60 / #62. Init bench shows mostly noise — mean +1.09% across 12 rows, range -3.7% to +7.1%. Unlike the SourceMapGenerator ctor change in #62 (which saw +8.7% on the addMapping bench, attributable to V8 hidden-class effects helping the downstream hot path), the consumer ctor isn't followed by a per-mapping hot loop in the same cycle — _parseMappings dominates init cost, so saving four function calls in the preamble is unmeasurable. Keeping it anyway for consistency with the codebase pattern and to prevent #62's followup readers from wondering why one ctor was inlined and the other was left alone. scripts/bench-diff.sh main trace (SOLO=1 PHASES=init): amp.js.map JSON -2.5% / Object -0.8% babel.min.js.map JSON +2.2% / Object +3.7% issue-41.js.map JSON -3.7% / Object 0.0% preact.js.map JSON +4.3% / Object +3.8% react.js.map JSON -1.6% / Object +1.5% vscode.map JSON -1.0% / Object +7.1% mean +1.09% Coverage holds: branch 97.16 / 98.75 (just above 97 / 98 thresholds). No new tests needed — existing fixtures already exercise the default branches. --- lib/source-map-consumer.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/source-map-consumer.js b/lib/source-map-consumer.js index a7cabc86..b8d962ac 100644 --- a/lib/source-map-consumer.js +++ b/lib/source-map-consumer.js @@ -332,12 +332,13 @@ function BasicSourceMapConsumer(aSourceMap, aSourceMapURL) { var version = util.getArg(sourceMap, 'version'); var sources = util.getArg(sourceMap, 'sources'); // Sass 3.3 leaves out the 'names' array, so we deviate from the spec (which - // requires the array) to play nice here. - var names = util.getArg(sourceMap, 'names', []); - var sourceRoot = util.getArg(sourceMap, 'sourceRoot', null); - var sourcesContent = util.getArg(sourceMap, 'sourcesContent', null); + // requires the array) to play nice here. Inlined getArg with default for + // the optional fields below — see #59 for the pattern. + var names = sourceMap.names != null ? sourceMap.names : []; + var sourceRoot = sourceMap.sourceRoot != null ? sourceMap.sourceRoot : null; + var sourcesContent = sourceMap.sourcesContent != null ? sourceMap.sourcesContent : null; var mappings = util.getArg(sourceMap, 'mappings'); - var file = util.getArg(sourceMap, 'file', null); + var file = sourceMap.file != null ? sourceMap.file : null; // Once again, Sass deviates from the spec and supplies the version as a // string rather than a number, so we use loose equality checking here.