Skip to content

Revert marginal micro-optimizations, keep only significant performance wins#4

Merged
pubkey merged 2 commits into
mainfrom
copilot/optimize-performance-significant-changes
Mar 20, 2026
Merged

Revert marginal micro-optimizations, keep only significant performance wins#4
pubkey merged 2 commits into
mainfrom
copilot/optimize-performance-significant-changes

Conversation

Copilot AI commented Mar 20, 2026

Copy link
Copy Markdown

The fork added many performance optimizations over upstream kofrasa/mingo. Some yield 5-20x improvements; others are micro-optimizations (charCode loops, indexed for-loops, manual string parsing) that marginally help while hurting readability. This PR reverts the marginal ones.

Benchmarks (upstream → all opts → significant only)

Benchmark Upstream All Opts This PR
Simple equality query (10K iter) 643ms 36ms 31ms
$elemMatch match (100K iter) 450ms 36ms 38ms
find().all() 100K docs 421ms 97ms 154ms
$match pipeline 10K docs 81ms 17ms 18ms
query.test() reuse 100K docs 944ms 182ms 197ms

~95% of gains retained. All 1,859 tests pass.

Reverted (marginal, hurt readability)

  • isOperator charCode loop → back to regex
  • isDigitStr charCode loop → compiled regex
  • charCodeAt(0) === 0x24[0] === "$"
  • typeOf explicit if-chain → original hash lookup
  • has, normalize manual for-loops → .every(), .some()
  • walk indexOf/substring → split/join
  • _hash.ts indexed loops → for...of
  • query.ts, Context.init Object.keys+index → Object.entries

Kept (significant wins)

  • isObject inline — bypasses typeOf overhead on the hottest path
  • resolve() fast paths — 1-segment and 2-segment short circuits, pre-split pathArray parameter
  • $elemMatch rewrite — compiles inner Query once at build time instead of per-document
  • Context.from() single-context fast path — skips allocation when only one context
  • $in Set fast path — O(n+m) via Set.has() for primitive arrays
  • mingoCmp number/string fast paths — skips typeOf for the two most common comparison types
  • isEqual for-loops — avoids closure overhead in hot equality checks
  • Native Set methods for intersection, unique, $setDifference, $setEquals, $setIsSubset
  • computeExpression — operator-first check avoids iterating all keys; indexOf replaces split for prefix extraction
  • Pre-split paths in processQuery, $exists

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…nificant performance wins

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor to retain only significant performance optimizations Revert marginal micro-optimizations, keep only significant performance wins Mar 20, 2026
Copilot AI requested a review from pubkey March 20, 2026 10:02
@pubkey
pubkey marked this pull request as ready for review March 20, 2026 10:13
@pubkey
pubkey merged commit 063d19f into main Mar 20, 2026
2 checks passed
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.

2 participants