Skip to content

Commit 40dea65

Browse files
committed
add Buffer.of documentation
1 parent f37d0cd commit 40dea65

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

doc/api/buffer.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,9 @@ A `TypeError` will be thrown if `size` is not a number.
827827
The `Buffer` module pre-allocates an internal `Buffer` instance of
828828
size [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new
829829
`Buffer` instances created using [`Buffer.allocUnsafe()`][], [`Buffer.from(array)`][],
830-
[`Buffer.from(string)`][], and [`Buffer.concat()`][] only when `size` is less than
831-
`Buffer.poolSize >>> 1` (floor of [`Buffer.poolSize`][] divided by two).
830+
[`Buffer.from(string)`][], [`Buffer.of(...items)`][], and [`Buffer.concat()`][]
831+
only when `size` is less than `Buffer.poolSize >>> 1` (floor of [`Buffer.poolSize`][]
832+
divided by two).
832833

833834
Use of this pre-allocated internal memory pool is a key difference between
834835
calling `Buffer.alloc(size, fill)` vs. `Buffer.allocUnsafe(size).fill(fill)`.
@@ -1419,6 +1420,37 @@ appropriate for `Buffer.from()` variants.
14191420
[`Buffer.from(string)`][] may also use the internal `Buffer` pool like
14201421
[`Buffer.allocUnsafe()`][] does.
14211422

1423+
### Static method: `Buffer.of(...items)`
1424+
1425+
<!-- YAML
1426+
added: v5.10.0
1427+
-->
1428+
1429+
* `...items` {integer} A sequence of numeric byte values (0–255).
1430+
* Returns: {Buffer}
1431+
1432+
Allocates a new `Buffer` from the given numeric arguments.
1433+
1434+
This is equivalent to the standard `TypedArray.of()` factory, but returns a
1435+
`Buffer` instead of a generic `Uint8Array`. Each argument provides the value of
1436+
the corresponding byte in the resulting buffer.
1437+
1438+
```mjs
1439+
import { Buffer } from 'node:buffer';
1440+
1441+
const buf = Buffer.of(0x62, 0x75, 0x66, 0x66, 0x65, 0x72);
1442+
```
1443+
1444+
```cjs
1445+
const { Buffer } = require('node:buffer');
1446+
1447+
const buf = Buffer.of(0x62, 0x75, 0x66, 0x66, 0x65, 0x72);
1448+
```
1449+
1450+
`Buffer.of(...items)` and [`Buffer.from(string)`][] may also use the internal
1451+
`Buffer` pool like [`Buffer.allocUnsafe()`][] does when the resulting size is
1452+
less than half of [`Buffer.poolSize`][].
1453+
14221454
### Static method: `Buffer.isBuffer(obj)`
14231455

14241456
<!-- YAML
@@ -5461,8 +5493,9 @@ to one of these new APIs._
54615493
potentially sensitive.
54625494

54635495
`Buffer` instances returned by [`Buffer.allocUnsafe()`][], [`Buffer.from(string)`][],
5464-
[`Buffer.concat()`][] and [`Buffer.from(array)`][] _may_ be allocated off a shared
5465-
internal memory pool if `size` is less than or equal to half [`Buffer.poolSize`][].
5496+
[`Buffer.of(...items)`][], [`Buffer.concat()`][], and [`Buffer.from(array)`][]
5497+
_may_ be allocated off a shared internal memory pool if `size` is less than or
5498+
equal to half [`Buffer.poolSize`][].
54665499
Instances returned by [`Buffer.allocUnsafeSlow()`][] _never_ use the shared internal
54675500
memory pool.
54685501

@@ -5515,6 +5548,7 @@ introducing security vulnerabilities into an application.
55155548
[`Buffer.from(arrayBuf)`]: #static-method-bufferfromarraybuffer-byteoffset-length
55165549
[`Buffer.from(buffer)`]: #static-method-bufferfrombuffer
55175550
[`Buffer.from(string)`]: #static-method-bufferfromstring-encoding
5551+
[`Buffer.of(...items)`]: #static-method-bufferofitems
55185552
[`Buffer.poolSize`]: #bufferpoolsize
55195553
[`ERR_INVALID_BUFFER_SIZE`]: errors.md#err_invalid_buffer_size
55205554
[`ERR_OUT_OF_RANGE`]: errors.md#err_out_of_range

doc/api/webstreams.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,8 @@ available.
693693
694694
Do not pass a pooled {Buffer} object instance in to this method.
695695
Pooled `Buffer` objects are created using `Buffer.allocUnsafe()`,
696-
or `Buffer.from()`, or are often returned by various `node:fs` module
697-
callbacks. These types of `Buffer`s use a shared underlying
696+
`Buffer.from()`, or `Buffer.of()` are often returned by various `node:fs`
697+
module callbacks. These types of `Buffer`s use a shared underlying
698698
{ArrayBuffer} object that contains all of the data from all of
699699
the pooled `Buffer` instances. When a `Buffer`, {TypedArray},
700700
or {DataView} is passed in to `readableStreamBYOBReader.read()`,

0 commit comments

Comments
 (0)