Skip to content

Commit

Permalink
fix(core): avoid slow stringification when checking for duplicates in…
Browse files Browse the repository at this point in the history
… dev mode (#58521)

When we check for duplicates in dev mode, we end up stringifying an `LView` even if we don't report an error. This can be expensive in large views.

These changes work around the issue by only generating the string when we have an error to throw.

Fixes #58509.

PR Close #58521
  • Loading branch information
crisbeto authored and pkozlowski-opensource committed Nov 6, 2024
1 parent 6c80778 commit 5f2d98a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/core/src/render3/list_reconciliation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import {TrackByFunction} from '../change_detection';
import {formatRuntimeError, RuntimeErrorCode} from '../errors';
import {assertNotSame} from '../util/assert';

import {stringifyForError} from './util/stringify_utils';

Expand Down Expand Up @@ -428,8 +427,12 @@ export class UniqueValueMultiKeyMap<K, V> {
set(key: K, value: V): void {
if (this.kvMap.has(key)) {
let prevValue = this.kvMap.get(key)!;
ngDevMode &&
assertNotSame(prevValue, value, `Detected a duplicated value ${value} for the key ${key}`);

// Note: we don't use `assertNotSame`, because the value needs to be stringified even if
// there is no error which can freeze the browser for large values (see #58509).
if (ngDevMode && prevValue === value) {
throw new Error(`Detected a duplicated value ${value} for the key ${key}`);
}

if (this._vMap === undefined) {
this._vMap = new Map();
Expand Down

0 comments on commit 5f2d98a

Please sign in to comment.