From e0ef2e8f9ae5eae7f2345aea49373290faf514d6 Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Mon, 11 May 2026 02:21:00 +0400 Subject: [PATCH] perf: stable Mapping hidden class in _parseMappings --- lib/source-map-consumer.js | 58 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/source-map-consumer.js b/lib/source-map-consumer.js index b1784072..e3716d57 100644 --- a/lib/source-map-consumer.js +++ b/lib/source-map-consumer.js @@ -633,7 +633,7 @@ BasicSourceMapConsumer.prototype._parseMappings = var index = 0; var temp = {}; var generatedMappings = []; - var mapping, value, charCode; + var value, charCode; // Reuse segment array to avoid allocations per mapping var segment = [0, 0, 0, 0, 0]; var segmentLength = 0; @@ -653,15 +653,6 @@ BasicSourceMapConsumer.prototype._parseMappings = index++; } else { - mapping = { - generatedLine: generatedLine, - generatedColumn: 0, - source: null, - originalLine: null, - originalColumn: null, - name: null - }; - // Decode VLQ values until we hit a separator segmentLength = 0; while (index < length) { @@ -690,33 +681,42 @@ BasicSourceMapConsumer.prototype._parseMappings = throw new Error('Found a source and line, but no column'); } - // Generated column. - mapping.generatedColumn = previousGeneratedColumn + segment[0]; - previousGeneratedColumn = mapping.generatedColumn; - + // Compute every mapping field into a local before allocating the + // literal. Building the object in one shot lets V8 settle on a + // single hidden class per mapping shape; the previous code wrote + // nulls into source/originalLine/originalColumn/name and then + // overwrote them with numbers, which trips per-field shape + // transitions and slows downstream reads (eachMapping iteration + // was the visible loser). + var genCol = previousGeneratedColumn + segment[0]; + previousGeneratedColumn = genCol; + + var src = null, origLine = null, origCol = null, nameIdx = null; if (segmentLength > 1) { - // Original source. - mapping.source = previousSource + segment[1]; - previousSource += segment[1]; + src = previousSource + segment[1]; + previousSource = src; - // Original line. - mapping.originalLine = previousOriginalLine + segment[2]; - previousOriginalLine = mapping.originalLine; - // Lines are stored 0-based - mapping.originalLine += 1; + var origLine0 = previousOriginalLine + segment[2]; + previousOriginalLine = origLine0; + origLine = origLine0 + 1; // stored 1-based - // Original column. - mapping.originalColumn = previousOriginalColumn + segment[3]; - previousOriginalColumn = mapping.originalColumn; + origCol = previousOriginalColumn + segment[3]; + previousOriginalColumn = origCol; if (segmentLength > 4) { - // Original name. - mapping.name = previousName + segment[4]; - previousName += segment[4]; + nameIdx = previousName + segment[4]; + previousName = nameIdx; } } - generatedMappings.push(mapping); + generatedMappings.push({ + generatedLine: generatedLine, + generatedColumn: genCol, + source: src, + originalLine: origLine, + originalColumn: origCol, + name: nameIdx + }); } }