Skip to content

Commit a7c75f9

Browse files
HarshithaKPBethGriggs
authored andcommitted
doc: document buffer.from returns internal pool buffer
Fixes: #22139 Co-authored-by: Mritunjay Goutam <[email protected]> PR-URL: #32703 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 63f0dd1 commit a7c75f9

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

doc/api/buffer.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,10 @@ A `TypeError` will be thrown if `size` is not a number.
379379

380380
The `Buffer` module pre-allocates an internal `Buffer` instance of
381381
size [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new
382-
`Buffer` instances created using [`Buffer.allocUnsafe()`][] and the deprecated
383-
`new Buffer(size)` constructor only when `size` is less than or equal to
384-
`Buffer.poolSize >> 1` (floor of [`Buffer.poolSize`][] divided by two).
382+
`Buffer` instances created using [`Buffer.allocUnsafe()`][],
383+
[`Buffer.from(array)`][], and the deprecated `new Buffer(size)` constructor only
384+
when `size` is less than or equal to `Buffer.poolSize >> 1` (floor of
385+
[`Buffer.poolSize`][] divided by two).
385386

386387
Use of this pre-allocated internal memory pool is a key difference between
387388
calling `Buffer.alloc(size, fill)` vs. `Buffer.allocUnsafe(size).fill(fill)`.
@@ -571,6 +572,9 @@ const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
571572
A `TypeError` will be thrown if `array` is not an `Array` or other type
572573
appropriate for `Buffer.from()` variants.
573574

575+
`Buffer.from(array)` and [`Buffer.from(string)`][] may also use the internal
576+
`Buffer` pool like [`Buffer.allocUnsafe()`][] does.
577+
574578
### Class Method: `Buffer.from(arrayBuffer[, byteOffset[, length]])`
575579
<!-- YAML
576580
added: v5.10.0
@@ -2733,10 +2737,11 @@ to one of these new APIs.*
27332737
uninitialized, the allocated segment of memory might contain old data that is
27342738
potentially sensitive.
27352739

2736-
`Buffer` instances returned by [`Buffer.allocUnsafe()`][] *may* be allocated off
2737-
a shared internal memory pool if `size` is less than or equal to half
2738-
[`Buffer.poolSize`][]. Instances returned by [`Buffer.allocUnsafeSlow()`][]
2739-
*never* use the shared internal memory pool.
2740+
`Buffer` instances returned by [`Buffer.allocUnsafe()`][] and
2741+
[`Buffer.from(array)`][] *may* be allocated off a shared internal memory pool
2742+
if `size` is less than or equal to half [`Buffer.poolSize`][]. Instances
2743+
returned by [`Buffer.allocUnsafeSlow()`][] *never* use the shared internal
2744+
memory pool.
27402745

27412746
### The `--zero-fill-buffers` command line option
27422747
<!-- YAML

0 commit comments

Comments
 (0)