Skip to content

perf: speed up fare filtering and sorting (patch attached) #39

Description

@simplyjackfoster

I don't have push access, so here's a patch for a Fares performance improvement: https://gist.github.com/simplyjackfoster/5917e657c0717758011c5cc4805d4003

To apply it (keeps authorship and the commit message):

curl -sL https://gist.githubusercontent.com/simplyjackfoster/5917e657c0717758011c5cc4805d4003/raw/perf-fares-filtering.patch | git am

Happy to open a PR from a fork instead if you'd prefer.

Summary

Moves the Fares pipeline into pure functions in src/faresPipeline.js and derives it with useMemo instead of chained effects and timeouts.

  • Precompute timestamps, hours, weekdays and route/amenity sets once per option so filters compare numbers instead of calling dayjs per check
  • Filter outbound and return options before pairing round trips, leaving only cross-leg checks in the pairing loop
  • Drop the three structuredClone passes by building new objects instead of mutating
  • Look up stations by id with a Map
  • Only recompute fares when travelers, fare class, strict or points change; other filters just re-pair
  • Build the price graph and date grid with Map lookups
  • Remove the artificial setTimeout delays and isInitialized counter
  • Refilter when toggling points (usePoints was missing from deps)

Results

  • Output is identical to the previous implementation across 1,224 randomized filter combinations
  • A 30-day flexible round trip with no filters drops from ~8s to ~0.3s

Files changed: src/Fares.js, src/faresPipeline.js (new)

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions