Skip to content

Commit e18e63f

Browse files
committed
lib: cache parsed source maps to reduce memory footprint
This also improves performance to map the stack trace when the `Error.stack` is accessed.
1 parent bcc2d58 commit e18e63f

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/internal/source_map/source_map_cache.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayPrototypeMap,
55
JSONParse,
6+
ObjectAssign,
67
ObjectKeys,
78
ObjectGetOwnPropertyDescriptor,
89
ObjectPrototypeHasOwnProperty,
@@ -14,6 +15,9 @@ const {
1415

1516
function ObjectGetValueSafe(obj, key) {
1617
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
18+
if (desc === undefined) {
19+
return undefined;
20+
}
1721
return ObjectPrototypeHasOwnProperty(desc, 'value') ? desc.value : undefined;
1822
}
1923

@@ -310,22 +314,25 @@ function findSourceMap(sourceURL) {
310314
if (!SourceMap) {
311315
SourceMap = require('internal/source_map/source_map').SourceMap;
312316
}
313-
let sourceMap = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
314-
if (sourceMap === undefined) {
317+
let entry = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
318+
if (entry === undefined) {
315319
for (const value of getCjsSourceMapCache()) {
316320
const filename = ObjectGetValueSafe(value, 'filename');
317321
const cachedSourceURL = ObjectGetValueSafe(value, 'sourceURL');
318322
if (sourceURL === filename || sourceURL === cachedSourceURL) {
319-
sourceMap = {
320-
data: ObjectGetValueSafe(value, 'data')
321-
};
323+
entry = value;
322324
}
323325
}
324326
}
325-
if (sourceMap && sourceMap.data) {
326-
return new SourceMap(sourceMap.data);
327+
if (entry === undefined) {
328+
return undefined;
329+
}
330+
let sourceMap = ObjectGetValueSafe(entry, 'sourceMap');
331+
if (sourceMap === undefined) {
332+
sourceMap = new SourceMap(ObjectGetValueSafe(entry, 'data'));
333+
ObjectAssign(entry, { __proto__: null, sourceMap });
327334
}
328-
return undefined;
335+
return sourceMap;
329336
}
330337

331338
module.exports = {

0 commit comments

Comments
 (0)