The buffer length property and data length in nodejs

When working with arrays the length property is not really a good way to go about getting the data length of a string. The reason why is because of the nature of Unicode. However in nodejs when working with buffers the buffer length property of a buffer can be used to get the amount of memory that the buffer is taking up at least. In addition if buffers are used the right way buffer length can be used as a way to get the actual data size of a string. So this will be a quick post on the buffer length property in nodejs and some related topic when it comes to array length.

1 - Buffer length basic example

The length property of a buffer will give the size of the buffer in bytes. This will always be the amount of memory that is allocated for the buffer rather than the total number of elements in an array, or the number of characters in a string. So for a basic example of buffer length I just needed to create a buffer my any way possible and just use the length property to get the buffer length of that buffer.

1
2
let buff = Buffer.alloc(8);
console.log(buff.length); // 8

2 - Do not set buffer length to change buffer size

When it comes to Array length setting the length property is one way to go about trimming the size of an array. That might work okay with arrays, but you do not really want to do the same with buffers. So the buffer length property should be considered read only, and another way should be used to set the size of a buffer.

One way to go about changing the buffer length would be to just use a method like buffer slice to create a new buffer of the desired size and then set that to the variable that is being used.

1
2
3
4
let buff = Buffer.alloc(8);
console.log(buff.length); // 8
buff = buff.slice(0,4);
console.log(buff.length); // 4

3 - String length and Buffer length

So the length property of a string is sometimes used to find the data size of a string. This might work okay if the string is composed of characters with a value of 127 or lower, but if that is not the case it will result in values that are a little off. However using the buffer from method and the buffer length property should get correct results.

Here I have a string that is composed of 8 characters and when I use buffer from and buffer length it is indeed 8 bytes in size.

1
2
3
4
5
let str = 'jalapeno';
console.log(str); // 'jalapeno'
console.log(str.length); // 8
let buff = Buffer.from(str);
console.log(buff.length); // 8

So far the string length property seems like an okay way to know the data size of a string right? However lets looks at just one more example.

1
2
3
4
5
6
7
8
9
let str = 'jalape\u00f1o';
console.log(str); // jalapeƱo
// array length is 8
console.log(str.length);
// actual data size in bytes is 9
let buff = Buffer.from(str, 'utf8');
console.log(buff.length); // 9

Turns out that the String length property is not a good way to get the size of a string, I could get into the nature of Unicode to explain better why this is, but that is a whole other post maybe.