Skip to content

Commit 8ae84be

Browse files
crisbetopkozlowski-opensource
authored andcommitted
fix(core): avoid slow stringification when checking for duplicates in 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
1 parent b0711d7 commit 8ae84be

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/core/src/render3/list_reconciliation.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {TrackByFunction} from '../change_detection';
1010
import {formatRuntimeError, RuntimeErrorCode} from '../errors';
11-
import {assertNotSame} from '../util/assert';
1211

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

@@ -428,8 +427,12 @@ export class UniqueValueMultiKeyMap<K, V> {
428427
set(key: K, value: V): void {
429428
if (this.kvMap.has(key)) {
430429
let prevValue = this.kvMap.get(key)!;
431-
ngDevMode &&
432-
assertNotSame(prevValue, value, `Detected a duplicated value ${value} for the key ${key}`);
430+
431+
// Note: we don't use `assertNotSame`, because the value needs to be stringified even if
432+
// there is no error which can freeze the browser for large values (see #58509).
433+
if (ngDevMode && prevValue === value) {
434+
throw new Error(`Detected a duplicated value ${value} for the key ${key}`);
435+
}
433436

434437
if (this._vMap === undefined) {
435438
this._vMap = new Map();

0 commit comments

Comments
 (0)