Skip to content

Commit

Permalink
lib: cache parsed source maps to reduce memory footprint
Browse files Browse the repository at this point in the history
This also improves performance to map the stack trace when the
`Error.stack` is accessed.
  • Loading branch information
legendecas committed Jan 16, 2023
1 parent bcc2d58 commit e18e63f
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayPrototypeMap,
JSONParse,
ObjectAssign,
ObjectKeys,
ObjectGetOwnPropertyDescriptor,
ObjectPrototypeHasOwnProperty,
Expand All @@ -14,6 +15,9 @@ const {

function ObjectGetValueSafe(obj, key) {
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
if (desc === undefined) {
return undefined;
}
return ObjectPrototypeHasOwnProperty(desc, 'value') ? desc.value : undefined;
}

Expand Down Expand Up @@ -310,22 +314,25 @@ function findSourceMap(sourceURL) {
if (!SourceMap) {
SourceMap = require('internal/source_map/source_map').SourceMap;
}
let sourceMap = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
if (sourceMap === undefined) {
let entry = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL);
if (entry === undefined) {
for (const value of getCjsSourceMapCache()) {
const filename = ObjectGetValueSafe(value, 'filename');
const cachedSourceURL = ObjectGetValueSafe(value, 'sourceURL');
if (sourceURL === filename || sourceURL === cachedSourceURL) {
sourceMap = {
data: ObjectGetValueSafe(value, 'data')
};
entry = value;
}
}
}
if (sourceMap && sourceMap.data) {
return new SourceMap(sourceMap.data);
if (entry === undefined) {
return undefined;
}
let sourceMap = ObjectGetValueSafe(entry, 'sourceMap');
if (sourceMap === undefined) {
sourceMap = new SourceMap(ObjectGetValueSafe(entry, 'data'));
ObjectAssign(entry, { __proto__: null, sourceMap });
}
return undefined;
return sourceMap;
}

module.exports = {
Expand Down

0 comments on commit e18e63f

Please sign in to comment.