Repository navigation
Performance improvements with TC39 proposals JSON.parseBinary and ArrayBuffer.prototype.detach #865
Closed
Guthib-of-Dan
started this conversation in
Ideas
Replies: 2 comments 1 reply
|
Thanks for sharing. Ky intentionally stays very close to Fetch and currently delegates JSON parsing to the platform through Response.json(). I would not want Ky to add a custom parsing path or depend on stage 0 APIs directly. If something like JSON.parseBinary ends up shipping broadly, the ideal place for Ky to benefit would be the platform itself, for example Response.json() using it internally. I’m generally interested, but I don’t think Ky can be a useful adoption signal at this stage. |
0 replies
|
Thanks for your answer! So if Ky depends on Fetch, it is better to modify Response.json (or something like Undici Fetch) by integrating JSON.parseBinary inside + ArrayBuffer.prototype.detach to clear initial utf8 chunks. |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Both proposals are currently stage 0 and I am looking for support from libraries like this, Ky, which handle untrusted data from the network.
Links:
https://github.com/Guthib-of-Dan/proposal-json-parse-binary
https://github.com/Guthib-of-Dan/proposal-arraybuffer-detach
ArrayBuffer.prototype.detach - same as ArrayBuffer.prototype.transfer(0). It lets us clear underlying memory buffer, which is either not cleared by GC for a while (lazy), or we parsed payload and no longer need that buffer -> lower memory usage in the context of concurrency + up to 30% faster benchmarks for V8
JSON.parseBinary - replacement for TextDecoder + JSON.parse.
Firstly, it return { ok: true, value } | { ok: false, message: string }. We need no try-catch and no stack trace is ever generated (which is needed for debugging, but how do we debug payload that is not controlled by us?)
Secondly, instead of decoding binary UTF8 payload from network to JS string before parsing, we decode WHILE parsing. We can catch syntax errors of the payload before decoding it, so we get guaranteed validity of the data. Also, we would decode only strings. If payload contains many numbers, brackets - we waste cpu time by copying/decoding that to js string.
Lastly, it can work in conjunction with ArrayBuffer.prototype.detach, so we parse JSON payload and immediately clear initial buffer, so memory spike lasts for milliseconds. Win-win scenario.
What do you think, @sindresorhus ? I currently seek TC39 delegates, which don’t seem to accept my ideas before they get wide recognition, so if these were to be implemented, would Ky (and probably Got) use them?
All reactions