perf: avoid cloning args on every pre/post - #45
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4407e9e035
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| args = error.args; | ||
| newArgs = args.slice(); | ||
| numArgs = args.length; | ||
| if (options?.numCallbackParams != null) { | ||
| numArgs = options.numCallbackParams; |
There was a problem hiding this comment.
Rebuild
errorArgs after overwrite in error-handler flow
When an error-handling post middleware throws/callbacks Kareem.overwriteResult, this branch updates args and newArgs but then continues without refreshing errorArgs. That means the next error handler is called with stale pre-overwrite arguments, whereas the previous implementation recomputed args per middleware iteration. This can corrupt chained error-handling behavior whenever one handler overwrites the result and a later handler depends on the updated args.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pull request overview
Optimizes Kareem hook execution performance by reducing per-hook argument cloning in execPre() / execPost(), and adds benchmark scripts to measure the impact.
Changes:
- Removes per-pre-hook argument cloning in
execPre()and calls each pre hook with the current args array. - Refactors
execPost()to reuse a singlenewArgsarray and callback wrapper across post hooks, rebuilding only when needed (e.g.overwriteResult). - Adds
benchmarks/execPre.jsandbenchmarks/execPost.jsto measure pre/post hook execution cost.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| index.js | Removes per-hook arg cloning and refactors post-hook arg/callback handling for performance. |
| benchmarks/execPre.js | Adds a benchmark runner for execPre() scenarios. |
| benchmarks/execPost.js | Adds a benchmark runner for execPost() scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@hasezoey @AbdelrahmanHafez can you please take a look? |
hasezoey
left a comment
There was a problem hiding this comment.
Looks good to me, though consider resolving copilot's suggestions..
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
perf: use kareem 3.3.0 re: mongoosejs/kareem#45
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [mongoose](https://mongoosejs.com) ([source](https://redirect.github.com/Automattic/mongoose)) | [`9.4.1` → `9.5.0`](https://renovatebot.com/diffs/npm/mongoose/9.4.1/9.5.0) |  |  | --- ### Release Notes <details> <summary>Automattic/mongoose (mongoose)</summary> ### [`v9.5.0`](https://redirect.github.com/Automattic/mongoose/blob/HEAD/CHANGELOG.md#950--2026-04-20) [Compare Source](https://redirect.github.com/Automattic/mongoose/compare/9.4.1...9.5.0) \================== - feat(debug): add `timestamp` option to debug output [#​16216](https://redirect.github.com/Automattic/mongoose/issues/16216) [rejunp](https://redirect.github.com/rejunp) - feat(query): add `cloneUpdate` option to explicitly disable update cloning [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) [#​16202](https://redirect.github.com/Automattic/mongoose/issues/16202) - feat(query): extend `defaults` query option to `find()` [#​16226](https://redirect.github.com/Automattic/mongoose/issues/16226) [sderrow](https://redirect.github.com/sderrow) - fix(query): avoid cloning update until absolutely necessary to better support updates with `__proto__` [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) [#​16202](https://redirect.github.com/Automattic/mongoose/issues/16202) - fix(query): avoid treating documents with a `$set()` method as objects with a `$set` property when casting updates [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) - fix(queryHelpers): pass default options to discriminators [#​16227](https://redirect.github.com/Automattic/mongoose/issues/16227) [#​16226](https://redirect.github.com/Automattic/mongoose/issues/16226) - fix(document): handle including and excluding nested paths with optimistic concurrency [#​16177](https://redirect.github.com/Automattic/mongoose/issues/16177) [#​16054](https://redirect.github.com/Automattic/mongoose/issues/16054) - fix(model): throw `ObjectParameterError` in `insertOne()` if doc is not an object [#​16221](https://redirect.github.com/Automattic/mongoose/issues/16221) [IshitaSingh0822](https://redirect.github.com/IshitaSingh0822) - fix(cast): preserve reason in `CastError` message after `setModel()` [#​16167](https://redirect.github.com/Automattic/mongoose/issues/16167) [White-Devil2839](https://redirect.github.com/White-Devil2839) - perf(model): remove unnecessary clone in `findOneAndUpdate()` [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) - perf: use kareem 3.3.0 [mongoosejs/kareem#45](https://redirect.github.com/mongoosejs/kareem/issues/45) [#​16229](https://redirect.github.com/Automattic/mongoose/issues/16229) - chore: use TSTyche assertions [#​16222](https://redirect.github.com/Automattic/mongoose/issues/16222) [mrazauskas](https://redirect.github.com/mrazauskas) </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/LUISDASARTIMANHAS/template-server-express-node-js). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMjMuOCIsInVwZGF0ZWRJblZlciI6IjQzLjEyMy44IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [mongoose](https://mongoosejs.com) ([source](https://redirect.github.com/Automattic/mongoose)) | [`^9.4.1` → `^9.5.0`](https://renovatebot.com/diffs/npm/mongoose/9.4.1/9.5.0) |  |  | --- ### Release Notes <details> <summary>Automattic/mongoose (mongoose)</summary> ### [`v9.5.0`](https://redirect.github.com/Automattic/mongoose/blob/HEAD/CHANGELOG.md#950--2026-04-20) [Compare Source](https://redirect.github.com/Automattic/mongoose/compare/9.4.1...9.5.0) \================== - feat(debug): add `timestamp` option to debug output [#​16216](https://redirect.github.com/Automattic/mongoose/issues/16216) [rejunp](https://redirect.github.com/rejunp) - feat(query): add `cloneUpdate` option to explicitly disable update cloning [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) [#​16202](https://redirect.github.com/Automattic/mongoose/issues/16202) - feat(query): extend `defaults` query option to `find()` [#​16226](https://redirect.github.com/Automattic/mongoose/issues/16226) [sderrow](https://redirect.github.com/sderrow) - fix(query): avoid cloning update until absolutely necessary to better support updates with `__proto__` [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) [#​16202](https://redirect.github.com/Automattic/mongoose/issues/16202) - fix(query): avoid treating documents with a `$set()` method as objects with a `$set` property when casting updates [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) - fix(queryHelpers): pass default options to discriminators [#​16227](https://redirect.github.com/Automattic/mongoose/issues/16227) [#​16226](https://redirect.github.com/Automattic/mongoose/issues/16226) - fix(document): handle including and excluding nested paths with optimistic concurrency [#​16177](https://redirect.github.com/Automattic/mongoose/issues/16177) [#​16054](https://redirect.github.com/Automattic/mongoose/issues/16054) - fix(model): throw `ObjectParameterError` in `insertOne()` if doc is not an object [#​16221](https://redirect.github.com/Automattic/mongoose/issues/16221) [IshitaSingh0822](https://redirect.github.com/IshitaSingh0822) - fix(cast): preserve reason in `CastError` message after `setModel()` [#​16167](https://redirect.github.com/Automattic/mongoose/issues/16167) [White-Devil2839](https://redirect.github.com/White-Devil2839) - perf(model): remove unnecessary clone in `findOneAndUpdate()` [#​16230](https://redirect.github.com/Automattic/mongoose/issues/16230) - perf: use kareem 3.3.0 [mongoosejs/kareem#45](https://redirect.github.com/mongoosejs/kareem/issues/45) [#​16229](https://redirect.github.com/Automattic/mongoose/issues/16229) - chore: use TSTyche assertions [#​16222](https://redirect.github.com/Automattic/mongoose/issues/16222) [mrazauskas](https://redirect.github.com/mrazauskas) </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/antoinezanardi/goat-it-api). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMjMuOCIsInVwZGF0ZWRJblZlciI6IjQzLjEyMy44IiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6W119--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Cloning args on every pre/post was a big drain on performance, especially for
pre()where we don't have to add callbacks anymore.Before:
After:
With post, the difference is less pronounced because we still have lingering callbacks. But still non-trival, before:
After:
This difference is significant enough to show up in Mongoose's
saveSimple()benchmark, which we have been striving to optimize.