Skip to content

Commit cdf0805

Browse files
chore(memory): sort nodes by size
1 parent 136da6a commit cdf0805

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/McpResponse.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {WebMCPTool} from 'puppeteer-core';
99
import type {ParsedArguments} from './bin/chrome-devtools-mcp-cli-options.js';
1010
import {ConsoleFormatter} from './formatters/ConsoleFormatter.js';
1111
import {HeapSnapshotFormatter} from './formatters/HeapSnapshotFormatter.js';
12+
import {isNodeLike} from './formatters/HeapSnapshotFormatter.js';
1213
import {IssueFormatter} from './formatters/IssueFormatter.js';
1314
import {NetworkFormatter} from './formatters/NetworkFormatter.js';
1415
import {SnapshotFormatter} from './formatters/SnapshotFormatter.js';
@@ -945,8 +946,14 @@ Call ${handleDialog.name} to handle it before continuing.`);
945946
}
946947
const nodes = this.#heapSnapshotOptions.nodes;
947948
if (nodes) {
949+
// Sort nodes by retained size in descending order.
950+
// We filter for nodes (ignoring edges if any) to satisfy TypeScript and ensure safe sorting.
951+
const sortedItems = nodes.items
952+
.filter(isNodeLike)
953+
.sort((a, b) => b.selfSize - a.selfSize);
954+
948955
const paginationData = this.#dataWithPagination(
949-
nodes.items,
956+
sortedItems,
950957
this.#heapSnapshotOptions.pagination,
951958
);
952959

src/formatters/HeapSnapshotFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface FormattedSnapshotEntry {
1616
retainedSize: number;
1717
}
1818

19-
function isNodeLike(
19+
export function isNodeLike(
2020
item: unknown,
2121
): item is DevTools.HeapSnapshotModel.HeapSnapshotModel.Node {
2222
return (

tests/tools/memory.test.js.snapshot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ id,name,type,distance,selfSize,retainedSize
167167
25307,"Array",object,2,192,2056
168168
33187,"Array",object,2,192,1664
169169
36255,"Array",object,2,192,1664
170-
45899,"Array",object,5,56,56
170+
46355,"Array",object,2,192,2056
171171
45901,"Array",object,5,88,88
172-
46149,"Array",object,5,56,56
173172
46151,"Array",object,5,88,88
174-
46355,"Array",object,2,192,2056
173+
45899,"Array",object,5,56,56
174+
46149,"Array",object,5,56,56
175175
Showing 1-8 of 8 (Page 1 of 1).
176176
`;
177177

0 commit comments

Comments
 (0)