Skip to content

Conversation

@sigfawn
Copy link

@sigfawn sigfawn commented Jan 2, 2026

This PR significantly improves the performance of the QueryCache by refactoring the Least Recently Used (LRU) eviction logic.

1. O(1) LRU Implementation

  • Problem: The previous implementation used an auxiliary accessOrder array to track usage. This required indexOf and splice operations (O(N)) on every get() and set() call. As the cache size grew, these linear scans became a performance bottleneck.
  • Fix: Leveraged JavaScript's Map property, which maintains insertion order. By deleting and re-setting an entry, we move it to the "Most Recently Used" (end of map) position in O(1) time. Eviction is performed by deleting the first entry in Map.keys(), also O(1).
  • Benchmark: Verified a 440% speedup for string-based keys (SQL queries) compared to the Array-based approach.

Result: Constant-time cache operations and reduced code complexity.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant