From ab65b7f9fe9607f84846b3f7c2d93d92e421c619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Fri, 31 Oct 2025 09:59:58 +0100 Subject: [PATCH] buffer: add bufferPool to Buffer.of --- benchmark/buffers/buffer-of.js | 23 +++++++++++++++++++ doc/api/buffer.md | 41 ++++++++++++++++++++++++++++++---- doc/api/webstreams.md | 4 ++-- lib/buffer.js | 25 ++++++++++++++++----- 4 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 benchmark/buffers/buffer-of.js diff --git a/benchmark/buffers/buffer-of.js b/benchmark/buffers/buffer-of.js new file mode 100644 index 00000000000000..5a40c00724b247 --- /dev/null +++ b/benchmark/buffers/buffer-of.js @@ -0,0 +1,23 @@ +'use strict'; + +const common = require('../common.js'); + +// Measure Buffer.of(...items) throughput for various lengths. +// We prebuild the items array to avoid measuring array construction, +// and vary the effective iterations with length to keep total work reasonable. + +const bench = common.createBenchmark(main, { + len: [0, 1, 8, 64, 256, 1024], + n: [5e5], +}); + +function main({ len, n }) { + const items = new Array(len); + for (let i = 0; i < len; i++) items[i] = i & 0xFF; + + bench.start(); + for (let i = 0; i < n; i++) { + Buffer.of(...items); + } + bench.end(n); +} diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 34e408453249cd..7631ca8078f32f 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -827,8 +827,9 @@ A `TypeError` will be thrown if `size` is not a number. The `Buffer` module pre-allocates an internal `Buffer` instance of size [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new `Buffer` instances created using [`Buffer.allocUnsafe()`][], [`Buffer.from(array)`][], -[`Buffer.from(string)`][], and [`Buffer.concat()`][] only when `size` is less than -`Buffer.poolSize >>> 1` (floor of [`Buffer.poolSize`][] divided by two). +[`Buffer.from(string)`][], [`Buffer.of(...items)`][], and [`Buffer.concat()`][] +only when `size` is less than `Buffer.poolSize >>> 1` (floor of [`Buffer.poolSize`][] +divided by two). Use of this pre-allocated internal memory pool is a key difference between calling `Buffer.alloc(size, fill)` vs. `Buffer.allocUnsafe(size).fill(fill)`. @@ -1419,6 +1420,36 @@ appropriate for `Buffer.from()` variants. [`Buffer.from(string)`][] may also use the internal `Buffer` pool like [`Buffer.allocUnsafe()`][] does. +### Static method: `Buffer.of(...items)` + + + +* `...items` {integer} A sequence of numeric byte values (0–255). +* Returns: {Buffer} + +Creates a new `Buffer` from the given numeric arguments. + +This is equivalent to the standard `TypedArray.of()` factory, but returns a +`Buffer` instead of a generic `Uint8Array`. Each argument provides the value of +the corresponding byte in the resulting buffer. + +```mjs +import { Buffer } from 'node:buffer'; + +const buf = Buffer.of(0x62, 0x75, 0x66, 0x66, 0x65, 0x72); +``` + +```cjs +const { Buffer } = require('node:buffer'); + +const buf = Buffer.of(0x62, 0x75, 0x66, 0x66, 0x65, 0x72); +``` + +[`Buffer.of(...items)`][] may also use the internal `Buffer` pool like +[`Buffer.allocUnsafe()`][] does. + ### Static method: `Buffer.isBuffer(obj)`