⚡ optimize ZodObject.parse loop performance#90
Conversation
Replaced the inefficient `for...in` loop in `ZodObject.parse` with a standard `for` loop iterating over cached keys. The keys are now pre-computed and stored in `this._keys` during `ZodObject` construction. Measured Improvement: - Baseline: ~524ms for 1,000,000 iterations - Optimized: ~499ms for 1,000,000 iterations - Improvement: ~4.8% reduction in execution time Verified with existing unit tests and a new performance benchmark. Co-authored-by: ekayaprod <225721478+ekayaprod@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What:
Optimized the
ZodObject.parsemethod injs/validation.jsby caching the schema's keys in the constructor and replacing thefor...inloop with a standard indexedforloop.🎯 Why:
The
for...inloop has more overhead in JavaScript as it traverses the prototype chain and is generally slower than iterating over a pre-defined array of keys. SinceZodObjectschemas are defined once and parsed many times, caching the keys significantly improves the performance of the validation hot path.📊 Measured Improvement:
Using the new
tests/validation_perf.mjsbenchmark (1,000,000 iterations):✅ Verification:
tests/validation.test.mjsand other related test files; all tests passed.PR created automatically by Jules for task 16492123602279516016 started by @ekayaprod