diff --git a/packages/core/src/utils/prepareEvent.ts b/packages/core/src/utils/prepareEvent.ts index f51b4afe4212..71bbe004908b 100644 --- a/packages/core/src/utils/prepareEvent.ts +++ b/packages/core/src/utils/prepareEvent.ts @@ -1,4 +1,4 @@ -import type { ClientOptions, Event, EventHint, StackParser } from '@sentry/types'; +import type { ClientOptions, Event, EventHint, StackFrame, StackParser } from '@sentry/types'; import { dateTimestampInSeconds, GLOBAL_OBJ, normalize, resolvedSyncPromise, truncate, uuid4 } from '@sentry/utils'; import { DEFAULT_ENVIRONMENT } from '../constants'; @@ -114,6 +114,8 @@ function applyClientOptions(event: Event, options: ClientOptions): void { } } +const debugIdStackParserCache = new WeakMap>(); + /** * Applies debug metadata images to the event in order to apply source maps by looking up their debug ID. */ @@ -124,9 +126,26 @@ export function applyDebugMetadata(event: Event, stackParser: StackParser): void return; } + let debugIdStackFramesCache: Map; + const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser); + if (cachedDebugIdStackFrameCache) { + debugIdStackFramesCache = cachedDebugIdStackFrameCache; + } else { + debugIdStackFramesCache = new Map(); + debugIdStackParserCache.set(stackParser, debugIdStackFramesCache); + } + // Build a map of filename -> debug_id const filenameDebugIdMap = Object.keys(debugIdMap).reduce>((acc, debugIdStackTrace) => { - const parsedStack = stackParser(debugIdStackTrace); + let parsedStack: StackFrame[]; + const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace); + if (cachedParsedStack) { + parsedStack = cachedParsedStack; + } else { + parsedStack = stackParser(debugIdStackTrace); + debugIdStackFramesCache.set(debugIdStackTrace, parsedStack); + } + for (let i = parsedStack.length - 1; i >= 0; i--) { const stackFrame = parsedStack[i]; if (stackFrame.filename) {