Skip to content

Commit 2b06d66

Browse files
legendecasruyadorno
authored andcommitted
lib: cache parsed source maps to reduce memory footprint
This also improves performance to map the stack trace when the `Error.stack` is accessed. PR-URL: #46225 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c10e602 commit 2b06d66

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/internal/source_map/source_map_cache.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const {
1414

1515
function ObjectGetValueSafe(obj, key) {
1616
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
17+
if (desc === undefined) {
18+
return undefined;
19+
}
1720
return ObjectPrototypeHasOwnProperty(desc, 'value') ? desc.value : undefined;
1821
}
1922

@@ -141,6 +144,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
141144
const url = data ? null : sourceMapURL;
142145
if (cjsModuleInstance) {
143146
getCjsSourceMapCache().set(cjsModuleInstance, {
147+
__proto__: null,
144148
filename,
145149
lineLengths: lineLengths(content),
146150
data,
@@ -149,6 +153,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
149153
});
150154
} else if (isGeneratedSource) {
151155
const entry = {
156+
__proto__: null,
152157
lineLengths: lineLengths(content),
153158
data,
154159
url,
@@ -162,6 +167,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
162167
// If there is no cjsModuleInstance and is not generated source assume we are in a
163168
// "modules/esm" context.
164169
const entry = {
170+
__proto__: null,
165171
lineLengths: lineLengths(content),
166172
data,
167173
url,
@@ -296,6 +302,7 @@ function sourceMapCacheToObject() {
296302
function appendCJSCache(obj) {
297303
for (const value of getCjsSourceMapCache()) {
298304
obj[ObjectGetValueSafe(value, 'filename')] = {
305+
__proto__: null,
299306
lineLengths: ObjectGetValueSafe(value, 'lineLengths'),
300307
data: ObjectGetValueSafe(value, 'data'),
301308
url: ObjectGetValueSafe(value, 'url')
@@ -310,22 +317,25 @@ function findSourceMap(sourceURL) {
310317
if (!SourceMap) {
311318
SourceMap = require('internal/source_map/source_map').SourceMap;
312319
}
313-
let sourceMap = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
314-
if (sourceMap === undefined) {
320+
let entry = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
321+
if (entry === undefined) {
315322
for (const value of getCjsSourceMapCache()) {
316323
const filename = ObjectGetValueSafe(value, 'filename');
317324
const cachedSourceURL = ObjectGetValueSafe(value, 'sourceURL');
318325
if (sourceURL === filename || sourceURL === cachedSourceURL) {
319-
sourceMap = {
320-
data: ObjectGetValueSafe(value, 'data')
321-
};
326+
entry = value;
322327
}
323328
}
324329
}
325-
if (sourceMap && sourceMap.data) {
326-
return new SourceMap(sourceMap.data);
330+
if (entry === undefined) {
331+
return undefined;
332+
}
333+
let sourceMap = ObjectGetValueSafe(entry, 'sourceMap');
334+
if (sourceMap === undefined) {
335+
sourceMap = new SourceMap(ObjectGetValueSafe(entry, 'data'));
336+
entry.sourceMap = sourceMap;
327337
}
328-
return undefined;
338+
return sourceMap;
329339
}
330340

331341
module.exports = {

0 commit comments

Comments
 (0)