Apollo cache's `canonicalStringify` is slightly slow
Issue Description
When investigating my project's performance, I found that canonicalStringify is slightly slow, and I could implement a bit faster function.
(* The project's performance might not be affected by this issue, but I think it might be a bit valuable to improve this function's performance.)
The measurement for 50000 calls is: original is 428ms for average (20 times) and my implementation is 261ms for average. (Measured on my machine (11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz 2.70 GHz, 16.0 GB RAM) and Google Chrome 137.0.7151.120) My implementation is here: https://github.com/jet2jet/apollo-canonical-stringify-test/blob/main/src/main.mjs#L21
Link to Reproduction
https://github.com/jet2jet/apollo-canonical-stringify-test
Reproduction Steps
Clone the above repository, run npm start after npm install, open http://localhost:3000/ and click Start to measure.
@apollo/client version
3.13.8
Hi @jet2jet,
thanks for digging into this.
Generally, there is a lot of optimizations on canonicalStringify that ensures that future re-executions with a different order of properties will benefit from previous runs - that might make it slightly slower if all runs are in the same order.
So there might be a tradeoff there.
That said, I currently don't have the time to investigate this deeply, as we're in the final stretches of Apollo Client 3.14 and 4.0 and this doesn't seem time-critical (we always can get improvements like this out in a later minor).
I'll take a note to come back to this, but please also feel free to ping me after these two versions are released :)