Skip to content

Performance: FileStore.loadState() reads entire file into memory with 3× peak allocation #58

Description

@jonbaldie

Finding — Asymptotic time complexity: O(E)

Subsystem: Persist (src/persist.ts:50–86)

Input variable

E = events in persist.dat

Current complexity

  • Time: O(E)
  • Space: O(E) — with 3× peak memory: content.split("\n") creates O(E) strings, .filter(...) creates another O(E) array, .map(...) creates a third O(E) array of parsed objects (lines 73–75)

Cause

loadState() reads the entire file into memory via a chunked read loop, decodes it, then chains content.split("\n").filter(...).map(...) — creating three intermediate arrays before returning the result.

Evidence

Events Time (median)
1,000 0.31 ms
10,000 2.68 ms
20,000 5.51 ms

Linear. Not a bottleneck by itself, but it is the linear component that combines with the quadratic replay in load(). The 3× temporary array allocation is a space concern for large files.

Runtime: Deno 2.7.6, V8 14.6.202.9-rusty, darwin (Apple Silicon)

Remediation direction

Stream-parse line-by-line (O(1) peak memory per line, no intermediate arrays).

Confidence

High — source analysis + benchmark.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions