From 117c7cf9f3ea5eca39e84fbcb92d417760bc01ba Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Sun, 19 Jul 2026 16:28:42 +0100 Subject: [PATCH] perf: special case arrays in partialMatchKey MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This does 2 things: 1. If the values being compared are both arrays, iterate through them directly 2. Use a `for...of` instead of allocating an arrow function per recursive call The results: | Task name | Latency avg (ns) | Latency med (ns) | Throughput avg (ops/s) | Throughput med (ops/s) | Samples | | -- | -- | -- | -- | -- | -- | | 'partialMatchKey dev' | '38.21 ± 0.16%' | '42.00 ± 0.00' | '24035274 ± 0.00%' | '23809524 ± 1' | 13086480 | | 'partialMatchKey prod' | '102.05 ± 0.52%' | '84.00 ± 1.00' | '10289814 ± 0.02%' | '11904762 ± 143431' | 4899508 | --- .changeset/humble-tips-try.md | 5 +++++ packages/query-core/src/utils.ts | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .changeset/humble-tips-try.md diff --git a/.changeset/humble-tips-try.md b/.changeset/humble-tips-try.md new file mode 100644 index 00000000000..ecc28ea0aaa --- /dev/null +++ b/.changeset/humble-tips-try.md @@ -0,0 +1,5 @@ +--- +'@tanstack/query-core': patch +--- + +Improve `partialMatchKey` performance in query-core. diff --git a/packages/query-core/src/utils.ts b/packages/query-core/src/utils.ts index b97b2cc5a33..f442ab86fdc 100644 --- a/packages/query-core/src/utils.ts +++ b/packages/query-core/src/utils.ts @@ -256,7 +256,22 @@ export function partialMatchKey(a: any, b: any): boolean { } if (a && b && typeof a === 'object' && typeof b === 'object') { - return Object.keys(b).every((key) => partialMatchKey(a[key], b[key])) + if (Array.isArray(a) && Array.isArray(b)) { + for (let i = 0; i < b.length; i++) { + if (!partialMatchKey(a[i], b[i])) { + return false + } + } + return true + } + + const bKeys = Object.keys(b) + for (const key of bKeys) { + if (!partialMatchKey(a[key], b[key])) { + return false + } + } + return true } return false