GeezerVison is showing github.com — lib: optimize `prepareStackTrace` on builtin frames · nodejs/node@e4b795e
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: benchmark/fixtures/simple-error-stack.js +11-10Lines changed: 11 additions & 10 deletions Collapse file
Copy file name to clipboardExpand all lines: benchmark/fixtures/simple-error-stack.ts +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
5 | 5 | const lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; |
6 | 6 |
|
7 | 7 | function simpleErrorStack() { |
8 | | - try { |
9 | | - (lorem as any).BANG(); |
10 | | - } catch (e) { |
11 | | - return e.stack; |
12 | | - } |
| 8 | + [1].map(() => { |
| 9 | + try { |
| 10 | + (lorem as any).BANG(); |
| 11 | + } catch (e) { |
| 12 | + return e.stack; |
| 13 | + } |
| 14 | + }) |
13 | 15 | } |
14 | 16 |
|
15 | 17 | export { |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/source_map/prepare_stack_trace.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,9 +53,15 @@ function prepareStackTraceWithSourceMaps(error, trace) { |
53 | 53 | const sm = fileName === lastFileName ? |
54 | 54 | lastSourceMap : |
55 | 55 | 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) |
58 | 62 | if (sm) { |
| 63 | + lastSourceMap = sm; |
| 64 | + lastFileName = fileName; |
59 | 65 | return `${kStackLineAt}${serializeJSStackFrame(sm, callSite, trace[i + 1])}`; |
60 | 66 | } |
61 | 67 | } catch (err) { |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/source_map/source_map_cache.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -347,6 +347,11 @@ function findSourceMap(sourceURL) { |
347 | 347 | return undefined; |
348 | 348 | } |
349 | 349 |
|
| 350 | + // No source maps for builtin modules. |
| 351 | + if (sourceURL.startsWith('node:')) { |
| 352 | + return undefined; |
| 353 | + } |
| 354 | + |
350 | 355 | SourceMap ??= require('internal/source_map/source_map').SourceMap; |
351 | 356 | try { |
352 | 357 | if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) { |
|