lib: optimize `prepareStackTrace` on builtin frames · nodejs/node@e4b795e · GitHub
Skip to content

Commit e4b795e

Browse files
legendecasaduh95
authored andcommitted
lib: optimize prepareStackTrace on builtin frames
Only invalidates source map lookup cache when a new source map is found. This improves when user codes interleave with builtin functions, like `array.map`. PR-URL: #56299 Refs: #56296 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
1 parent d1b009b commit e4b795e

4 files changed

Lines changed: 31 additions & 17 deletions

File tree

benchmark/fixtures/simple-error-stack.js

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/fixtures/simple-error-stack.ts

Lines changed: 7 additions & 5 deletions

lib/internal/source_map/prepare_stack_trace.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ function prepareStackTraceWithSourceMaps(error, trace) {
5353
const sm = fileName === lastFileName ?
5454
lastSourceMap :
5555
findSourceMap(fileName);
56-
lastSourceMap = sm;
57-
lastFileName = fileName;
56+
// Only when a source map is found, cache it for the next iteration.
57+
// This is a performance optimization to avoid interleaving with JS builtin function
58+
// invalidating the cache.
59+
// - at myFunc (file:///path/to/file.js:1:2)
60+
// - at Array.map (<anonymous>)
61+
// - at myFunc (file:///path/to/file.js:3:4)
5862
if (sm) {
63+
lastSourceMap = sm;
64+
lastFileName = fileName;
5965
return `${kStackLineAt}${serializeJSStackFrame(sm, callSite, trace[i + 1])}`;
6066
}
6167
} catch (err) {

lib/internal/source_map/source_map_cache.js

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)