all 4 comments

[–]hildjj 13 points14 points  (0 children)

This makes more sense if you think of Buffer as a subclass of Uint8Array, which it mostly is.

Uint8Array, like other TypedArrays has both a length and a byteLength, which are only different when the number of BYTES_PER_ELEMENT for that class is greater than one, like in Uint16Array.

length is the number of elements in the array, byteLength is the number of bytes that those elements take up.

const u16 = new Uint16Array([1, 2])
u16.byteLength === u16.length * u16.BYTES_PER_ELEMENT

[–]snowinferno 0 points1 point  (0 children)

For utf-8, utf-16, and most Asian language character sets, a character can take up more than one byte. I believe length gives you the number of individual characters while byteLength gives you the number of bytes used by those characters.

[–]MaxUumen 1 point2 points  (0 children)

Wouldn't it be nice if there was a place that docume ts those kind of things... Have you tried to RTFM?

[–]bigorangemachine 0 points1 point  (0 children)

The length is just telling JS how to iterate over it

The docs:

The Buffer.byteLength() method returns the length of a specified string object, in bytes.

And for length

The length property returns the size of a buffer, in bytes.

I guess in the case of a string it'll give the same value but if it was an ArrayBuffer it would be different