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

Open Beginner friendly
#71 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
desktop

Research direction

Start in apps/desktop/src/main/usage/store.ts at lines 153–154 and trace how records are used to calculate spanMs. Replace the large-record calculation without relying on spread arguments or the intermediate array, then verify that records over 65,536 entries avoid the RangeError and still produce the correct span.

Written by the indexing model from the issue text.

Description

performance

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.

Dominant language
TypeScript
Stars
120
Forks
13
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from unbug/tday

All issues in unbug/tday

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.