Buffer

Buffer.byteLength()

Counts bytes required by an encoding rather than JavaScript UTF-16 code units. Tested on Node.js 22.16.0. For malformed hex or base64 text the count need not match decoding; validate input separately.

Formal Syntax & Structure

nodejs syntaxnodejs
Buffer.byteLength(string[, encoding])

Parameters & Attributes

NameTypeDescription
stringstring | Buffer | TypedArray | DataView | ArrayBuffer | SharedArrayBufferText or byte-backed value.
encodingstringString encoding; defaults to utf8.

Return Value

number

Byte count.

Working Example

Example Snippetnodejs
import { Buffer } from 'node:buffer';console.log('café'.length, Buffer.byteLength('café','utf8'));
Node.js Reference