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

Use JSON to pass data from SSR to improve performance #6168

Closed
CyberAP opened this issue Aug 1, 2019 · 8 comments
Closed

Use JSON to pass data from SSR to improve performance #6168

CyberAP opened this issue Aug 1, 2019 · 8 comments

Comments

@CyberAP
Copy link

CyberAP commented Aug 1, 2019

What problem does this feature solve?

Using JavaScript to pass data from SSR to an app through global context is not efficient.
JSON, on the other hand, offers a much better performance and could be used instead. Explanation in v8 blog, Benchmarks (performance increase up to 2 times)
This is especially important for apps with a lot of state.

What does the proposed changes look like?

This code:

<script>window.__NUXT__ = { data: {} /* some state */ }</script>

Turns into this:

<script id="NUXT_STATE" type="application/json">
{ data: {} }
</script>
// somewhere in app init
const stateEl = document.getElementById('NUXT_STATE');
const state = JSON.parse(stateEl.textContent);
app.state = state;
This feature request is available on Nuxt community (#c9586)
@ghost ghost added the cmty:feature-request label Aug 1, 2019
@Timkor
Copy link

Timkor commented Aug 2, 2019

What about circulair references? Or types which are not supported by JSON?

@CyberAP
Copy link
Author

CyberAP commented Aug 2, 2019

For circular references we have: https://github.com/WebReflection/circular-json
This could be enabled in a nuxt config for those who really need that.

For unsupported JSON types could you elaborate a little bit more? What's the usecase?

@manniL
Copy link
Member

manniL commented Aug 2, 2019

Thanks for the information! We read that article as well but things are a bit more complicated here. For Nuxt 2, we decided to go with devalue (our fork) instead of pure JSON.stringify for a couple for reasons. This won't change in Nuxt 2 to avoid breaking changes.

@CyberAP
Copy link
Author

CyberAP commented Aug 2, 2019

@manniL could you unveil these reasons if possible? I'm really curious when JSON is not appropriate as a DTO.

@aldarund
Copy link

aldarund commented Sep 28, 2019

circular-json seems to be replaced by flatted. I tested flatted and devalue on 120kb payload. Without circular.
devalue -> 1600 ops
flatted -> 630 ops
json.strigify -> 4900 ops
Reasons for devalue instead of json listed on the devalue page itself.

repeated references ([value, value])
undefined, Infinity, NaN, -0
regular expressions
dates
Map and Set

For example undefined values just got stripped by json.stringify

@404-NF
Copy link

404-NF commented Jan 5, 2020

@aldarund how about use json.strigify(value, function(key,value){ xxxx }) instead devalue;
use strigify replacer can support special types;
in my test, most of develop scene devalue takes 60%-70% more time than common json.stringify. it takes several times than render.
it's not only effct the time per request but also request per second. it looks like devalue become the most performance problem for me.

@JBustin
Copy link

JBustin commented Jul 20, 2020

Just for information, I notice the same type of performance issue with, for example, the script injection in vue-renderer :

    if (shouldInjectScripts || shouldHashCspScriptSrc) {
      serializedSession = `window.${this.serverContext.globals.context}=${devalue(renderContext.nuxt)};`;
    }

In my case, with devalue, ~10ms, with JSON.stringify, ~1ms.
I experiment some quickly event loop saturation and this kind of synchonous long and frequent tasks is painful.
At least, an async stringify operator for huge object should keep event loop safe.

@danielroe
Copy link
Member

This is now implemented in Nuxt 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants