Node.js Buffer API Changes

const jsonString = getJsonStringSomehow();
const myBuffer = Buffer(JSON.parse(jsonString));
Buffer([1,2,3])
{“type”:”Buffer”,”data”:[1,2,3]}
const jsonString = '100';
const myBuffer = Buffer(JSON.parse(jsonString));
// Allocate *initialized* memory. It will be zero-filled by default. 
// The optional fill and encoding parameters can be used to specify
// an alternate fill value.
Buffer.alloc(size[, fill[, encoding]])
// Allocate *uninitialized* memory
Buffer.allocUnsafe(size)
// Create a Buffer from a String, Array, Buffer, or ArrayBuffer
Buffer.from(str[, encoding])
Buffer.from(array)
Buffer.from(buffer)
Buffer.from(arrayBuffer[, offset[, length]])
$ node
> Buffer.allocUnsafe(10)
<Buffer 50 04 80 02 01 00 00 00 0a 00>
$ node --zero-fill-buffers
> Buffer.allocUnsafe(10)
<Buffer 00 00 00 00 00 00 00 00 00 00>

--

--

--

Open Source Architect, nearForm

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store