Description
Is your feature request related to a problem? Please describe.
TextEncoder.prototype.encodeInto
is a new method available in latest Chrome and Firefox that allows efficient encoding of strings into a given buffer, avoiding intermediate allocation and copying.
Its primary usecase is a more efficient way to pass strings to WASM (one of the most expensive things one has to do when communicating with JavaScript) and it's already leveraged by Rust wasm-bindgen where available. wasm-bindgen has support for a Node.js target, so it would be great to speed things up here too.
Describe the solution you'd like
Adding native encodeInto
method to the TextEncoder
class already available in util
module.
Please describe the desired behavior.
MDN: https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encodeInto
Describe alternatives you've considered
Sending PR to wasm-bindgen to wrap memory into a Node-specific Buffer
and use Buffer.prototype.write
instead.
It has very similar semantics, and produces similar speed-ups (up to 35% on some of my string-interaction-heavy WASM libraries), but I wanted to see if it's possible to solve this on the Node.js side first so that the same method could be used for all platforms.
cc @jasnell as the author of the original implementation of WHATWG Encoding API in Node.js (#13644)