graphql-anywhere: replace `JSON.stringify` call on very hot code path
See original GitHub issueHey, folks! We use the propType
module from graphql-anywhere
in quite a few places in our app & we’re finding that the JSON.stringify
call here causes a noticeable slowdown when we make a particularly large data request during local development (ie, when prop-type checking is performed).
The size of our returned data is relatively large and contains many nested types in its query, which I suppose is what’s causing this code path to be so hot.
Here’s what I see when using Chrome’s performance profiling feature after making a couple of these large network requests:
I replaced this JSON.stringify
call with a hard-coded string for comparison, which drove this line’s execution time to 10s of ms.
Using JSON.stringify
is nice for the level of detail it gives in the error, but would you accept an alternative that makes this line run faster? Using JSON.stringify
causes such a large slowdown during local development that I think we’ll have to remove this package from our app if a change isn’t possible.
A simple solution might be to just make the error ${info.resultKey} missing
.
Happy to make a PR if you’d be open to it!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top GitHub Comments
I’m open to improving this, but I would just point out that we (the Apollo Client team) thought we had written the code such that
JSON.stringify
would not be called in production, where performance matters most, thanks tots-invariant
androllup-plugin-invariant
.This issue is specifically pointing out that
JSON.stringify
is called unnecessarily in development, which is true, but the stringification payload needs to be pretty huge before that becomes a noticeable problem, and there’s no performance penalty in production, because long error messages are stripped in production builds.Also, just in case anyone who reads this is new to JavaScript performance,
JSON.stringify
is one of the most obsessively optimized tools we have available to us as web developers, so in general there’s no way to do what it does any faster with handwritten JS. You can only pass less data to it, or stop calling it unnecessarily, which is what we’re considering doing here.Closing since
graphql-anywhere
is no longer something we’re supporting (details: https://github.com/apollographql/apollo-client/issues/5744#issuecomment-571988792). Thanks!