Repository navigation
perf(query): avoid intermediate array allocations in stringifyQuery (… - #391
ayushdwivedi-stack wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthroughQuery parsing, encoding, and stringification now handle empty inputs and values with updated incremental output logic. Tests cover these behaviors, along with duplicate keys, URL encoding, and inherited properties. ChangesQuery utilities
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Refactor Merge Risk: ⚪ Minimal · up to This change speeds up query string building and parsing while keeping output the same, and it adds tests for empty values and segments. No merge-blocking risk was identified. Architecture SummaryArchitecture risk: 🟡 Medium · up to The change affects 2 systems. Changed systems: Architecture concerns Review detailsSystems and components
Before / after behavior
Reliability and maintainability
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
stringifyQueryto serialize query parameters in a single pass without intermediate array allocations (.filter().map().filter().join()).encodeQueryItemto encode the query key once when serializing array-valued parameters.parseQuery.stringifyQuery,encodeQueryItem, andparseQueryedge cases.Problem
stringifyQuerycurrently usesObject.keys(query).filter(...).map(...).filter(Boolean).join("&"), which allocates four intermediate arrays and iterates four times over keys on every call. In high-throughput applications (e.g. Nuxt / Nitro / h3 request pipelines), this adds unnecessary memory churn and garbage collection pressure.Solution
stringifyQuerywith a single loop overObject.keys(query), streaming string chunks directly into the output.encodeQueryItem, cached the encoded key once when formatting array values.parseQuery, added an early exit whenparametersStringis empty and skipped empty tokens (&&) without regex invocation.Benchmark
Local benchmark on 1,000,000 iterations:
stringifyQuery: ~66.4% faster (763 ms down to 458 ms), 0 intermediate array allocations.parseQuery: ~83.9% faster (1217 ms down to 662 ms).Testing
pnpm lint-> Prettier and ESLint passed cleanly.vitest run --typecheck-> All 14 test files, 505 tests, and TypeScript type checks passed with 0 errors.Issue reference
Closes #332
Summary by CodeRabbit