MCPcopy Index your code
hub / github.com/nodejs/node / byteLength

Function byteLength

lib/buffer.js:837–868  ·  view source on GitHub ↗
(string, encoding)

Source from the content-addressed store, hash-verified

835}
836
837function byteLength(string, encoding) {
838 if (typeof string !== 'string') {
839 if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
840 return string.byteLength;
841 }
842
843 throw new ERR_INVALID_ARG_TYPE(
844 'string', ['string', 'Buffer', 'ArrayBuffer'], string,
845 );
846 }
847
848 const len = string.length;
849 if (len === 0)
850 return 0;
851
852 if (!encoding || encoding === 'utf8') {
853 return byteLengthUtf8(string);
854 }
855
856 if (encoding === 'ascii') {
857 return len;
858 }
859
860 const ops = getEncodingOps(encoding);
861 if (ops === undefined) {
862 // TODO (ronag): Makes more sense to throw here.
863 // throw new ERR_UNKNOWN_ENCODING(encoding);
864 return byteLengthUtf8(string);
865 }
866
867 return ops.byteLength(string);
868}
869
870Buffer.byteLength = byteLength;
871

Callers

nothing calls this directly

Calls 1

getEncodingOpsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…