From ce878bbb8b08f7ee9dd1a94fbc80fa8c3fa70512 Mon Sep 17 00:00:00 2001 From: mritunjaygoutam12 Date: Mon, 12 Nov 2018 02:00:17 +0530 Subject: [PATCH 1/4] doc: Buffer.from will return internal pool buffer --- doc/api/buffer.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 1ad65494278b73..0be6c6480f577f 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -788,6 +788,27 @@ const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); A `TypeError` will be thrown if `array` is not an `Array`. +`Buffer.from(array)` will also use the internal `Buffer` pool if `array.length` is less than or equal to half [`Buffer.poolSize`] just like [`Buffer.allocUnsafe()`] does. + +```js +const nodeBuffers = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7,8, 9]); + +console.log(nodeBuffers.offset); +// prints: 344 + +// Default Buffer.poolsize is 8192 +console.log(Buffer.poolSize); +// prints: 8192 + +// But you can change it +Buffer.poolSize=5; + +const buf = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7,8, 9]); + +console.log(buf.offset); +// prints: 0 +``` + ### Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]])