Files
node/test/parallel/test-source-map-entry-indices.js
Jacky Zhao 3db9632585 module: store source map mappings in a typed array
SourceMap kept one six-element array per mapping segment. Bundled
applications have millions of segments, and with --enable-source-maps
a parsed map lives for the life of the process: a 63 MB source map with
2.9 million segments retained about 290 MiB of heap.

Store the segments in a single Int32Array, six fields per segment, and
resolve source URLs and names through per-map tables. Segments are
counted before parsing so storage is allocated once, and sorting is
skipped when the mappings are already in order. The public API and the
objects returned by findEntry() and findOrigin() are unchanged.

Signed-off-by: Jacky Zhao <j.zhao2k19@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66261
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2026-11-17 14:58:30 +00:00

71 lines
1.6 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { SourceMap } = require('node:module');
// Segments listed out of column order are looked up by generated position.
{
const sm = new SourceMap({
version: 3,
sources: ['a.js'],
names: [],
mappings: 'KAAA,LAAC',
});
assert.strictEqual(sm.findEntry(0, 0).originalColumn, 1);
assert.strictEqual(sm.findEntry(0, 6).originalColumn, 0);
}
// In an index map, a source or name index outside its own section's lists
// does not resolve to an entry of another section.
{
const sm = new SourceMap({
version: 3,
sections: [
{
offset: { line: 0, column: 0 },
map: {
version: 3,
sources: ['a.js'],
names: ['first'],
mappings: 'AAAAA,ECAAC',
},
},
{
offset: { line: 1, column: 0 },
map: {
version: 3,
sources: ['b.js'],
names: ['second'],
mappings: 'AAAAA',
},
},
],
});
assert.deepStrictEqual(sm.findEntry(0, 0), {
generatedLine: 0,
generatedColumn: 0,
originalSource: 'a.js',
originalLine: 0,
originalColumn: 0,
name: 'first',
});
assert.deepStrictEqual(sm.findEntry(0, 2), {
generatedLine: 0,
generatedColumn: 2,
originalSource: undefined,
originalLine: 0,
originalColumn: 0,
name: undefined,
});
assert.deepStrictEqual(sm.findEntry(1, 0), {
generatedLine: 1,
generatedColumn: 0,
originalSource: 'b.js',
originalLine: 0,
originalColumn: 0,
name: 'second',
});
}