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
| Name | Type | Description |
|---|---|---|
| string | string | Buffer | TypedArray | DataView | ArrayBuffer | SharedArrayBuffer | Text or byte-backed value. |
| encoding | string | String 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'));