How to get the string length in bytes in nodejs? If I have a string, like this: äáöü
then str.length will return with 4. But how to get that, how many bytes form the string?
-
3A string does not *have* a length in bytes. This depends on the encoding used. – usr Mar 25 '12 at 22:38
6 Answers
Here is an example:
str = 'äáöü';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// äáöü: 4 characters, 8 bytes
-
1Is there a way to automatically get KB, MB, as appropriate etc (human readable size) – chovy Sep 11 '13 at 20:56
-
4
function getBytes(string){
return Buffer.byteLength(string, 'utf8')
}

- 13,434
- 14
- 60
- 80
-
16
-
1Buffer.byteLength is already a such function, and example above at least shows it's usage. With the same luck you could do `var byteLength = Buffer.byteLength` and it would also work just the same. – RReverser Oct 22 '15 at 18:27
-
2
Alternatively, you can use TextEncoder
new TextEncoder().encode(str).length
Related question
Assume it's slower though

- 1,341
- 19
- 21
This depends where the string is.
In JavaScript engines (at least, in most of them, including V8, used by Node.js and Chromium/Chrome), strings are encoded as UTF-16 internally. In UTF-16 encoding, every character is either 2 or 4 bytes long. Every character that's common in any major human language (and many that aren't) are encoded in 2 bytes (one code unit), while characters from rarer languages, emoji, and unusual symbols are often encoded in 4 bytes (two code units).
Moreover, the JavaScript string length
property actually does not return the number of characters in the string, it returns the number of code units. For example, ''.length
returns 2 even though the string contains only one character.
Finally, the strings are almost certainly (though I have not checked) null-terminated, so throw on an extra 2 bytes for that.
Putting it together, the length of a string residing in your Node.js script's memory is (str.length * 2) + 2
bytes.
On the other hand, when you send a string in an HTTP request, or write it to a file, it will typically be converted by default to UTF-8 before being transmitted to its destination. Characters in UTF-8 can be 1, 2, 3, or 4 bytes long (not counting the phenomenon of "over-long characters" and potential future expansion).
For this, I have nothing to add on top of the other answers to this question, which show how to calculate the length of a string in UTF-8.

- 3,189
- 15
- 24
If you want to specific encoded, here is iconv
example
var iconv = require('iconv-lite');
var buf =iconv.encode('äáöü', 'utf8');
console.log(buf.length);
// output: 8

- 1