Skip to content

perf(usage-store): Math.max(...tsValues) spread overflows V8 argument stack on large record sets #71

@github-actions

Description

@github-actions

Problem

apps/desktop/src/main/usage/store.ts L153–154

const tsValues = records.map((r) => r.ts); // intermediate array
const spanMs = tsValues.length > 1
  ? Math.max(...tsValues) - Math.min(...tsValues) // ← spread
  : 0;

Math.max(...array) passes array elements as function arguments via spread. V8's function call argument limit is ~65,536. When records exceeds that count, a RangeError: Maximum call stack size exceeded is thrown.

Suggested fix

Replace the spread with a reduce or for loop:

let minTs = Infinity, maxTs = -Infinity;
for (const r of records) {
  if (r.ts < minTs) minTs = r.ts;
  if (r.ts > maxTs) maxTs = r.ts;
}

This also eliminates the intermediate tsValues array allocation, saving one O(n) pass.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions