Skip to content

sessions.json uses JSON5.parse unnecessarily — 35x slower than JSON.parse #14529

@hyf0-agent

Description

@hyf0-agent

Summary

loadSessionStore() in src/config/sessions/store.ts uses JSON5.parse() to read sessions.json, but the file is always written by JSON.stringify() (standard JSON). JSON5 parsing is ~35x slower than native JSON.parse for the same input.

Evidence

Write path (standard JSON)

// src/config/sessions/store.ts — saveSessionStoreUnlocked()
const json = JSON.stringify(store, null, 2);
await fs.promises.writeFile(tmp, json, { mode: 0o600, encoding: "utf-8" });

Read path (JSON5 — unnecessary)

// src/config/sessions/store.ts — loadSessionStore()
import JSON5 from "json5";
// ...
const parsed = JSON5.parse(raw);

Since JSON.stringify only produces valid standard JSON (no comments, no trailing commas, no unquoted keys), JSON5.parse provides no benefit here.

Benchmark Results

Synthetic sessions.json files with realistic session entries (~0.7 KB each), 20 iterations per size, median reported:

Entries Size (MB) JSON.parse (ms) JSON5.parse (ms) Slowdown
50 0.04 0.05 2.29 46x
100 0.07 0.10 4.58 46x
250 0.18 0.33 11.58 35x
500 0.35 0.75 24.72 33x
1,000 0.70 1.46 52.12 36x
2,000 1.41 2.38 99.09 42x
5,000 3.52 5.90 244.76 41x
10,000 7.05 11.91 491.64 41x
20,000 14.11 27.26 1,052.78 39x

At 5,000 entries (3.5 MB), JSON5.parse takes ~245ms vs ~6ms for JSON.parse.

In a real-world incident, 250 sessions with large transcripts produced a 61 MB file — at that scale, JSON5.parse would take multiple seconds per read.

Real-world Impact

Suggested Fix

Replace JSON5.parse with JSON.parse in loadSessionStore():

- const parsed = JSON5.parse(raw);
+ const parsed = JSON.parse(raw);

Optionally, add a JSON5.parse fallback for backwards compatibility with any hand-edited files:

let parsed;
try {
  parsed = JSON.parse(raw);
} catch {
  // Fallback for hand-edited files that may use JSON5 syntax
  parsed = JSON5.parse(raw);
}

This is a one-line change with zero risk for machine-generated files and a 35x performance improvement on the hot path.

Environment

  • Node.js v22.22.0
  • json5@2.x
  • Linux x64 (KVM VM)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions