-
Notifications
You must be signed in to change notification settings - Fork 516
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
CBOR vs. JSON performance -- why is CBOR.encode so much slower than JSON.stringify? #2542
Comments
With the JSON fast path enabled the JSON encoding is relatively well optimized, CBOR is not refined to such an extent which explains some of the difference. In principle it should be trivial to CBOR encode strings, but since CBOR strings need to be pure UTF-8 there's an "is this valid UTF-8" check for the strings during encoding. This is now a string scan before the encode can proceed. It should be possible to optimize this to match JSON.stringify performance. However, in master there's (soon) an even simpler fix: with WTF-8 support |
My test object actually contained a fair amount of strings. Thanks for the information, looked into duk__cbor_encode_string_top and I noticed you already have some test options DUK_CBOR_TEXT_STRINGS / DUK_CBOR_BYTE_STRINGS. Looks like I will be able to make it skip the utf8 check for my usage using one of these #defines. I have to say, I'm really looking forward to future duktape updates. I hope you are doing well. Is there any way to donate to show support? |
If you happen to test CBOR performance with DUK_CBOR_{TEXT,BYTE}_STRINGS enabled, it'd be nice to know how much impact that has. The same level of overhead would be eliminated after the "string is UTF8" flag is added to
Thanks! It's been a bit difficult finding time for Duktape in the past few years but slowly things are getting better. There's no active donation method right now, but a good way to give support is to provide concrete, reproducible and actionable issues and pulls :-) |
My test json: 100k iterations I haven't looked any closer. I will try to research it properly one day. Luckily I mostly use decode in my project, and that beats JSON so I'm happy.. but hopeflly CBOR encode can be as fast as JSON stringify some day, or close to it. #define DUK_CBOR_DECODE_FASTPATH #define DUK_CBOR_TEXT_STRINGS #define DUK_CBOR_BYTE_STRINGS |
Thanks for the measurements 👍
This is probably because when decoding back, CBOR byte strings will decode into Uint8Array objects which are much heavier than strings. |
In my tests CBOR.encode is around ~3x slower than JSON.stringify, but CBOR.decode is ~3.7x faster.
Would be incredible if CBOR.encode could achieve similar performance as JSON.stringify.
Has anyone looked into this more closely? I've tried various json objects and the numbers are around the same every time.
The text was updated successfully, but these errors were encountered: