Skip to content

Commit 1661ee4

Browse files
committed
Optimize large JSON body formatting
1 parent e35966c commit 1661ee4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/services/ui-worker-formatters.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,21 @@ const WorkerFormatters = {
7575
},
7676
json: (content: Buffer) => {
7777
const asString = content.toString('utf8');
78-
return formatJson(asString, { formatRecords: false });
78+
79+
// Do simplify parse + stringify where possible for speed - it's up to 1000x faster.
80+
// We fall back to the relaxed formatJson() where that fails, which is slower but
81+
// always comes up with something reasonable - unless it's very large, in which
82+
// case we give up rather than hanging the UI:
83+
try {
84+
return JSON.stringify(JSON.parse(asString), null, 2);
85+
} catch (e) {
86+
if (content.byteLength <= 5_000_000) {
87+
return formatJson(asString, { formatRecords: false });
88+
} else {
89+
// Large non-parseable content - we fall back to the raw string
90+
return asString;
91+
}
92+
}
7993
},
8094
'json-records': (content: Buffer) => {
8195
const asString = content.toString('utf8');

0 commit comments

Comments
 (0)