Skip to content

Commit 5224301

Browse files
committed
optimise #getFullCallstack with #fullCallstackCacheTrace
1 parent beb136c commit 5224301

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/wrapper/TraceUtil.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class TraceUtil {
3939
callstackType: EWrapperCallstackType = EWrapperCallstackType.FULL;
4040
trace4Debug: string | null = null;
4141
trace4Bypass: string | null = null;
42+
#fullCallstackCacheTrace: Map</*traceId*/ string, TTrace[]> = new Map();
4243
static readonly SIGNATURE = 'browser-api-monitor';
4344

4445
constructor() {
@@ -82,13 +83,22 @@ export class TraceUtil {
8283
}
8384

8485
#getFullCallstack(e: Error, uniqueTrait?: unknown): TCallstack {
85-
const trace = this.#getFullTrace(e.stack || '');
86-
const traceId = e.stack || String(uniqueTrait);
86+
const traceId = hashString(e.stack || String(uniqueTrait));
87+
const cached = this.#fullCallstackCacheTrace.get(traceId);
88+
let rv;
8789

88-
return {
89-
traceId: hashString(traceId),
90-
trace: trace || [this.#getInvalidTrace(uniqueTrait)],
91-
};
90+
if (cached) {
91+
rv = { traceId, trace: cached };
92+
} else {
93+
const trace = this.#getFullTrace(e.stack || '');
94+
rv = {
95+
traceId,
96+
trace: trace || [this.#getInvalidTrace(uniqueTrait)],
97+
};
98+
this.#fullCallstackCacheTrace.set(traceId, rv.trace);
99+
}
100+
101+
return rv;
92102
}
93103

94104
#getFullTrace(stackString: string): TTrace[] | null {
@@ -112,13 +122,13 @@ export class TraceUtil {
112122
let traceId;
113123

114124
if (trace) {
115-
traceId = trace.link;
125+
traceId = hashString(trace.link);
116126
} else {
117-
traceId = e.stack || String(uniqueTrait);
127+
traceId = hashString(e.stack || String(uniqueTrait));
118128
trace = this.#getInvalidTrace(uniqueTrait);
119129
}
120130

121-
return { traceId: hashString(traceId), trace: [trace] };
131+
return { traceId, trace: [trace] };
122132
}
123133

124134
#getShortTrace(stackString: string): TTrace | null {

0 commit comments

Comments
 (0)