1- import type { ClientOptions , Event , EventHint , StackParser } from '@sentry/types' ;
1+ import type { ClientOptions , Event , EventHint , StackFrame , StackParser } from '@sentry/types' ;
22import { dateTimestampInSeconds , GLOBAL_OBJ , normalize , resolvedSyncPromise , truncate , uuid4 } from '@sentry/utils' ;
33
44import { DEFAULT_ENVIRONMENT } from '../constants' ;
@@ -118,6 +118,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void {
118118 }
119119}
120120
121+ const debugIdStackParserCache = new WeakMap < StackParser , Map < string , StackFrame [ ] > > ( ) ;
122+
121123/**
122124 * Applies debug metadata images to the event in order to apply source maps by looking up their debug ID.
123125 */
@@ -128,9 +130,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void
128130 return ;
129131 }
130132
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+
131142 // Build a map of filename -> debug_id
132143 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+
134153 for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
135154 const stackFrame = parsedStack [ i ] ;
136155 if ( stackFrame . filename ) {
0 commit comments