1
- import type { ClientOptions , Event , EventHint , StackParser } from '@sentry/types' ;
1
+ import type { ClientOptions , Event , EventHint , StackFrame , StackParser } from '@sentry/types' ;
2
2
import { dateTimestampInSeconds , GLOBAL_OBJ , normalize , resolvedSyncPromise , truncate , uuid4 } from '@sentry/utils' ;
3
3
4
4
import { DEFAULT_ENVIRONMENT } from '../constants' ;
@@ -118,6 +118,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void {
118
118
}
119
119
}
120
120
121
+ const debugIdStackParserCache = new WeakMap < StackParser , Map < string , StackFrame [ ] > > ( ) ;
122
+
121
123
/**
122
124
* Applies debug metadata images to the event in order to apply source maps by looking up their debug ID.
123
125
*/
@@ -128,9 +130,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void
128
130
return ;
129
131
}
130
132
133
+ let debugIdStackFramesCache : Map < string , StackFrame [ ] > ;
134
+ const cachedDebugIdStackFrameCache = debugIdStackParserCache . get ( stackParser ) ;
135
+ if ( cachedDebugIdStackFrameCache ) {
136
+ debugIdStackFramesCache = cachedDebugIdStackFrameCache ;
137
+ } else {
138
+ debugIdStackFramesCache = new Map < string , StackFrame [ ] > ( ) ;
139
+ debugIdStackParserCache . set ( stackParser , debugIdStackFramesCache ) ;
140
+ }
141
+
131
142
// Build a map of filename -> debug_id
132
143
const filenameDebugIdMap = Object . keys ( debugIdMap ) . reduce < Record < string , string > > ( ( acc , debugIdStackTrace ) => {
133
- const parsedStack = stackParser ( debugIdStackTrace ) ;
144
+ let parsedStack : StackFrame [ ] ;
145
+ const cachedParsedStack = debugIdStackFramesCache . get ( debugIdStackTrace ) ;
146
+ if ( cachedParsedStack ) {
147
+ parsedStack = cachedParsedStack ;
148
+ } else {
149
+ parsedStack = stackParser ( debugIdStackTrace ) ;
150
+ debugIdStackFramesCache . set ( debugIdStackTrace , parsedStack ) ;
151
+ }
152
+
134
153
for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
135
154
const stackFrame = parsedStack [ i ] ;
136
155
if ( stackFrame . filename ) {
0 commit comments