Skip to content

Commit 46a7681

Browse files
Whitecxtargos
authored andcommitted
doc: add Buffer.from(string) to functions that use buffer pool
Buffer.from(string) is one of the functions that may use the pre-allocated buffer. It's mentioned in the description of Buffer.from(array), but not in Buffer.from(string), or in the two other places where functions that behave this way are listed, so this commit adds those references. PR-URL: #52801 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f077afa commit 46a7681

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

doc/api/buffer.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ A `TypeError` will be thrown if `size` is not a number.
808808
The `Buffer` module pre-allocates an internal `Buffer` instance of
809809
size [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new
810810
`Buffer` instances created using [`Buffer.allocUnsafe()`][], [`Buffer.from(array)`][],
811-
and [`Buffer.concat()`][] only when `size` is less than
811+
[`Buffer.from(string)`][], and [`Buffer.concat()`][] only when `size` is less than
812812
`Buffer.poolSize >>> 1` (floor of [`Buffer.poolSize`][] divided by two).
813813

814814
Use of this pre-allocated internal memory pool is a key difference between
@@ -846,11 +846,11 @@ _may contain sensitive data_. Use [`buf.fill(0)`][`buf.fill()`] to initialize
846846
such `Buffer` instances with zeroes.
847847

848848
When using [`Buffer.allocUnsafe()`][] to allocate new `Buffer` instances,
849-
allocations under 4 KiB are sliced from a single pre-allocated `Buffer`. This
850-
allows applications to avoid the garbage collection overhead of creating many
851-
individually allocated `Buffer` instances. This approach improves both
852-
performance and memory usage by eliminating the need to track and clean up as
853-
many individual `ArrayBuffer` objects.
849+
allocations less than `Buffer.poolSize >>> 1` (4KiB when default poolSize is used) are sliced
850+
from a single pre-allocated `Buffer`. This allows applications to avoid the
851+
garbage collection overhead of creating many individually allocated `Buffer`
852+
instances. This approach improves both performance and memory usage by
853+
eliminating the need to track and clean up as many individual `ArrayBuffer` objects.
854854

855855
However, in the case where a developer may need to retain a small chunk of
856856
memory from a pool for an indeterminate amount of time, it may be appropriate
@@ -1390,6 +1390,9 @@ console.log(buf1.toString('latin1'));
13901390
A `TypeError` will be thrown if `string` is not a string or another type
13911391
appropriate for `Buffer.from()` variants.
13921392

1393+
[`Buffer.from(string)`][] may also use the internal `Buffer` pool like
1394+
[`Buffer.allocUnsafe()`][] does.
1395+
13931396
### Static method: `Buffer.isBuffer(obj)`
13941397

13951398
<!-- YAML
@@ -5444,10 +5447,10 @@ to one of these new APIs._
54445447
uninitialized, the allocated segment of memory might contain old data that is
54455448
potentially sensitive.
54465449

5447-
`Buffer` instances returned by [`Buffer.allocUnsafe()`][] and
5448-
[`Buffer.from(array)`][] _may_ be allocated off a shared internal memory pool
5449-
if `size` is less than or equal to half [`Buffer.poolSize`][]. Instances
5450-
returned by [`Buffer.allocUnsafeSlow()`][] _never_ use the shared internal
5450+
`Buffer` instances returned by [`Buffer.allocUnsafe()`][], [`Buffer.from(string)`][],
5451+
[`Buffer.concat()`][] and [`Buffer.from(array)`][] _may_ be allocated off a shared
5452+
internal memory pool if `size` is less than or equal to half [`Buffer.poolSize`][].
5453+
Instances returned by [`Buffer.allocUnsafeSlow()`][] _never_ use the shared internal
54515454
memory pool.
54525455

54535456
### The `--zero-fill-buffers` command-line option

0 commit comments

Comments
 (0)