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.
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.
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.
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.
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.
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.