byte-counter
Version:
Count bytes passing through a stream
14 lines (10 loc) • 302 B
JavaScript
const textEncoder = new TextEncoder();
export function byteLength(data) {
if (typeof data === 'string') {
return textEncoder.encode(data).byteLength;
}
if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer || data instanceof SharedArrayBuffer) {
return data.byteLength;
}
return 0;
}