From 9c32f785f2dab4cd3e4e94c75165b277126b00bd Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 6 Sep 2023 12:04:55 +0200 Subject: [PATCH] ref(core): Avoid unnecessary breadcrumbs array mutations This is a micro improvement, but maybe worth it as we can have a lot of breadcrumbs. This ensures we only create a new breadcrumbs array if we exceed the limit. In contrast, currently we'll always create two copies of the breadcrumbs for each added breadcrumb (first for the array spread, then we copy it through the slice), even if we don't really need to do this. --- packages/core/src/scope.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/core/src/scope.ts b/packages/core/src/scope.ts index 8875f1c996f2..b2342169a510 100644 --- a/packages/core/src/scope.ts +++ b/packages/core/src/scope.ts @@ -419,7 +419,11 @@ export class Scope implements ScopeInterface { timestamp: dateTimestampInSeconds(), ...breadcrumb, }; - this._breadcrumbs = [...this._breadcrumbs, mergedBreadcrumb].slice(-maxCrumbs); + + const breadcrumbs = this._breadcrumbs; + breadcrumbs.push(mergedBreadcrumb); + this._breadcrumbs = breadcrumbs.length > maxCrumbs ? breadcrumbs.slice(-maxCrumbs) : breadcrumbs; + this._notifyScopeListeners(); return this;