buf.length
Returns the amount of memory allocated for the Buffer in number of bytes. Note that this does not necessarily reflect the amount of usable data within the Buffer. For instance, in the example below, a Buffer with 1234 bytes is allocated, but only 11 ASCII bytes are written.
const buf = Buffer.allocUnsafe(1234); console.log(buf.length); // Prints: 1234 buf.write('some string', 0, 'ascii'); console.log(buf.length); // Prints: 1234
While the length
property is not immutable, changing the value of length
can result in undefined and inconsistent behavior. Applications that wish to modify the length of a Buffer should therefore treat length
as read-only and use buf.slice()
to create a new Buffer.
var buf = Buffer.allocUnsafe(10); buf.write('abcdefghj', 0, 'ascii'); console.log(buf.length); // Prints: 10 buf = buf.slice(0,5); console.log(buf.length); // Prints: 5
Please login to continue.