blitz-js / superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
MIT License
3.88k stars 83 forks source link

Improve performance #25

Open merelinguist opened 3 years ago

merelinguist commented 3 years ago

Recent benchmarks against a similar library, devalue, read as follows:

superjson output: 179 bytes
devalue output: 116 bytes

() => superjson.stringify(obj)
1000000 iterations in 21005ms

() => devalue(obj)
1000000 iterations in 8168ms

() => superjson.parse(superjson_serialized)
1000000 iterations in 6487ms

() => eval(`(${devalue_serialized})`)
1000000 iterations in 2290ms

I’m not so concerned about the output size difference, which I think for now is negligible, but more about the speed. Obviously we’re making different design choices here which necessarily are probably slower, but I think it’d be good to see what improvements can be made in this area.

0.021005ms is pretty quick, though!

Thanks to Rich for setting up the comparison: https://github.com/Rich-Harris/superjson-and-devalue

Skn0tt commented 3 years ago

Yeah, we'll definitely need to look into performance. I think there's a lot of low-hanging fruits to be reaped by changing stuff like [...path, head] to path.push(head) if that's possible.

Skn0tt commented 3 years ago

We should tackle blitz-js/blitz#10 before this to have some objective measurement I guess :D

merelinguist commented 3 years ago

Yep! Was going to just comment on that but thought I’d set up a new issue.

flybayer commented 3 years ago

One thing to keep in mind, is that on the server string<>object serialization & deserialization is currently handled by Next. So we won't use superjson.stringify, but we will on the client.

That said, we could use superjson.stringify on the server and bypass the Next res.json() helper.

Skn0tt commented 3 years ago

I’m not so concerned about the output size difference, which I think for now is negligible, but more about the speed.

In my opinion, we should focus the other way around. UX-wise, compute cycles are cheap but network usage is expensive.

flybayer commented 3 years ago

So we already have a slow perf report from someone using Blitz, albeit to load 20k items in one request :)

https://github.com/blitz-js/legacy-framework/issues/778

Skn0tt commented 3 years ago

Uh-oh ... I guess we‘ll need to look into adding a bigger benchmark case ^^

sannajammeh commented 10 months ago

Is this still the case?