diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index 9502cfef6fe029..ca841f1de954f5 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -1,6 +1,7 @@ 'use strict'; const { + SafeMap, ArrayPrototypeIndexOf, ArrayPrototypeJoin, ArrayPrototypeMap, @@ -170,6 +171,8 @@ function getErrorSource( return exceptionLine; } +// Only load source from disk once: +const sourcesCache = new SafeMap(); function getOriginalSource(payload, originalSourcePath) { let source; const originalSourcePathNoScheme = @@ -181,11 +184,14 @@ function getOriginalSource(payload, originalSourcePath) { // First we check if the original source content was provided in the // source map itself: source = payload.sourcesContent[sourceContentIndex]; + } else if (sourcesCache.has(originalSourcePathNoScheme)) { + return sourcesCache.get(originalSourcePathNoScheme); } else { // If no sourcesContent was found, attempt to load the original source // from disk: try { source = readFileSync(originalSourcePathNoScheme, 'utf8'); + sourcesCache.set(originalSourcePathNoScheme, source); } catch (err) { debug(err); source = '';