I have a Buffer
instance which contains utf-8 JSON.
Usually you convert it this way:
const buffer = Buffer.from('{"a":1}')
const str = buffer.toString("utf-8")
const obj = JSON.parse(str)
To make the Buffer->Object conversion more performant how would I convert it without an intermediate string?
Buffers have a toString() method that you can use to convert the buffer to a string. By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer. from() , the toString() function gives you the original string back.
In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.
The Buffer. toJSON() method returns the buffer in JSON format. Note: The JSON. Stringify() is the method which can also be used to return the data in JSON format.
js buffer copy() Method.
The JSON.parse
can accept Buffer
instances.
const buffer = Buffer.from('{"a":1}')
const obj = JSON.parse(buffer)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With