diff --git a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.spec.ts b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.spec.ts index ea6e896e8c0..3da71f5c4f8 100644 --- a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.spec.ts +++ b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.spec.ts @@ -183,4 +183,40 @@ describe('mergeFrames', () => { source: '', }); }); + + it('should not mutate the merged frames', () => { + const createElement = (name: string): ElementProfile => ({ + children: [], + directives: [ + { + isComponent: false, + isElement: false, + lifecycle: {}, + outputs: {}, + changeDetection: 10, + name, + }, + ], + type: 'element', + }); + const createFrame = (directives: ElementProfile[]): ProfilerFrame => ({ + directives, + duration: 5, + source: 'foo', + }); + + // Only the later frames have the `Bar` element, e.g. because it was created after the first one. + const frames = [ + createFrame([createElement('Foo')]), + createFrame([createElement('Foo'), createElement('Bar')]), + createFrame([createElement('Foo'), createElement('Bar')]), + ]; + const framesBefore = structuredClone(frames); + + const result = mergeFrames(frames); + + expect(frames).toEqual(framesBefore); + expect(mergeFrames(frames)).toEqual(result); + expect(result!.directives[1].directives[0].changeDetection).toBe(20); + }); }); diff --git a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.ts b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.ts index 9cff1f9079b..91e8e521fdf 100644 --- a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.ts +++ b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/recording-timeline/record-formatter/frame-merger.ts @@ -45,7 +45,8 @@ const mergeDirectives = (mergeIn: ElementProfile[], second: ElementProfile[]) => if (mergeInDirective && mergeInDirective.name === d.name) { mergeDirective(mergeInDirective, d); } else { - mergeIn[i].directives.push(d); + // Copy, so merging later frames into it doesn't change the recorded frame. + mergeIn[i].directives.push(structuredClone(d)); } }); mergeDirectives(mergeIn[i].children, second[i].children);