Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): remove duplicate JSON.stringify call on data in ssrExchange #3632

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-goats-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Removes double serialization of `data` in `ssrExchange`
1 change: 0 additions & 1 deletion packages/core/src/exchanges/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ const serializeResult = (
includeExtensions: boolean
): SerializedResult => {
const serialized: SerializedResult = {
data: JSON.stringify(result.data),
Copy link
Collaborator

@JoviDeCroock JoviDeCroock Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer removing the if branch then as JSON.stringify(undefined) still has a result. This PR also needs a changeset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, calling JSON.stringify with undefined is slower
https://jsperf.app/vuwofo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SerializedResult type allows the data to be a string only or undefined. deserializeResult will return undefined if there is no data.

data: result.data ? JSON.parse(result.data) : undefined,

Looking at the implementation closer, I'd like to use something more efficient than JSON.stringify, like devalue. Because I can reduce 2mb JSON to 1.1MB, I just checked this out

hasNext: result.hasNext,
};

Expand Down