1
- import type { DebugImage , StackFrame , StackParser } from '@sentry/types' ;
1
+ import type { DebugImage , StackParser } from '@sentry/types' ;
2
2
import { GLOBAL_OBJ } from './worldwide' ;
3
3
4
- const debugIdStackParserCache = new WeakMap < StackParser , Map < string , StackFrame [ ] > > ( ) ;
4
+ type StackString = string ;
5
+ type CachedResult = [ string , string ] ;
6
+
7
+ let parsedStackResults : Record < StackString , CachedResult > | undefined ;
8
+ let lastKeysCount : number | undefined ;
9
+ let cachedFilenameDebugIds : Record < string , string > | undefined ;
5
10
6
11
/**
7
12
* Returns a map of filenames to debug identifiers.
@@ -12,38 +17,46 @@ export function getFilenameToDebugIdMap(stackParser: StackParser): Record<string
12
17
return { } ;
13
18
}
14
19
15
- let debugIdStackFramesCache : Map < string , StackFrame [ ] > ;
16
- const cachedDebugIdStackFrameCache = debugIdStackParserCache . get ( stackParser ) ;
17
- if ( cachedDebugIdStackFrameCache ) {
18
- debugIdStackFramesCache = cachedDebugIdStackFrameCache ;
19
- } else {
20
- debugIdStackFramesCache = new Map < string , StackFrame [ ] > ( ) ;
21
- debugIdStackParserCache . set ( stackParser , debugIdStackFramesCache ) ;
20
+ const debugIdKeys = Object . keys ( debugIdMap ) ;
21
+
22
+ // If the count of registered globals hasn't changed since the last call, we
23
+ // can just return the cached result.
24
+ if ( cachedFilenameDebugIds && debugIdKeys . length === lastKeysCount ) {
25
+ return cachedFilenameDebugIds ;
22
26
}
23
27
28
+ lastKeysCount = debugIdKeys . length ;
29
+
24
30
// Build a map of filename -> debug_id.
25
- return Object . keys ( debugIdMap ) . reduce < Record < string , string > > ( ( acc , debugIdStackTrace ) => {
26
- let parsedStack : StackFrame [ ] ;
31
+ cachedFilenameDebugIds = debugIdKeys . reduce < Record < string , string > > ( ( acc , stackKey ) => {
32
+ if ( ! parsedStackResults ) {
33
+ parsedStackResults = { } ;
34
+ }
35
+
36
+ const result = parsedStackResults [ stackKey ] ;
27
37
28
- const cachedParsedStack = debugIdStackFramesCache . get ( debugIdStackTrace ) ;
29
- if ( cachedParsedStack ) {
30
- parsedStack = cachedParsedStack ;
38
+ if ( result ) {
39
+ acc [ result [ 0 ] ] = result [ 1 ] ;
31
40
} else {
32
- parsedStack = stackParser ( debugIdStackTrace ) ;
33
- debugIdStackFramesCache . set ( debugIdStackTrace , parsedStack ) ;
34
- }
41
+ const parsedStack = stackParser ( stackKey ) ;
35
42
36
- for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
37
- const stackFrame = parsedStack [ i ] ;
38
- const file = stackFrame && stackFrame . filename ;
43
+ for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
44
+ const stackFrame = parsedStack [ i ] ;
45
+ const filename = stackFrame && stackFrame . filename ;
46
+ const debugId = debugIdMap [ stackKey ] ;
39
47
40
- if ( stackFrame && file ) {
41
- acc [ file ] = debugIdMap [ debugIdStackTrace ] as string ;
42
- break ;
48
+ if ( filename && debugId ) {
49
+ acc [ filename ] = debugId ;
50
+ parsedStackResults [ stackKey ] = [ filename , debugId ] ;
51
+ break ;
52
+ }
43
53
}
44
54
}
55
+
45
56
return acc ;
46
57
} , { } ) ;
58
+
59
+ return cachedFilenameDebugIds ;
47
60
}
48
61
49
62
/**
@@ -55,6 +68,10 @@ export function getDebugImagesForResources(
55
68
) : DebugImage [ ] {
56
69
const filenameDebugIdMap = getFilenameToDebugIdMap ( stackParser ) ;
57
70
71
+ if ( ! filenameDebugIdMap ) {
72
+ return [ ] ;
73
+ }
74
+
58
75
const images : DebugImage [ ] = [ ] ;
59
76
for ( const path of resource_paths ) {
60
77
if ( path && filenameDebugIdMap [ path ] ) {
0 commit comments