Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
doc: Buffer.from will return internal pool buffer
  • Loading branch information
mritunjayz committed Nov 11, 2018
commit ce878bbb8b08f7ee9dd1a94fbc80fa8c3fa70512
21 changes: 21 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not always correct. It may return 0, and any subsequent call will probably increase it.
So it doesn't always print 344.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// prints: 344
// Prints: 344


// Default Buffer.poolsize is 8192
console.log(Buffer.poolSize);
// prints: 8192
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// prints: 8192
// 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// prints: 0
// Prints: 0

```

### Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]])
<!-- YAML
added: v5.10.0
Expand Down