Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: buffer.from will return internal pool buffer #24312

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
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
23 changes: 23 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,29 @@ 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.
mritunjayz marked this conversation as resolved.
Show resolved Hide resolved
mritunjayz marked this conversation as resolved.
Show resolved Hide resolved

```js
const nodeBuffers = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
mritunjayz marked this conversation as resolved.
Show resolved Hide resolved

console.log(nodeBuffers.offset);
// prints: 344
Copy link
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
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
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;
Copy link
Member

Choose a reason for hiding this comment

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

This could be misleading. Changing Buffer.poolSize does have immediate effect on which buffers would be pooled, but it doesn't immediately change the size of the current pool, it changes the size of the next pool.
It does, however, take immediate effect for determining whether should the buffer be allocated from the pool or not.


const buf = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
mritunjayz marked this conversation as resolved.
Show resolved Hide resolved

console.log(buf.offset);
// prints: 0
Copy link
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