79

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?

1
  • 3
    A string does not have a length in bytes. This depends on the encoding used. Commented Mar 25, 2012 at 22:38

6 Answers 6

166

Here is an example:

str = 'äáöü';

console.log(str + ": " + str.length + " characters, " +
  Buffer.byteLength(str, 'utf8') + " bytes");

// äáöü: 4 characters, 8 bytes

Buffer.byteLength(string, [encoding])

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to automatically get KB, MB, as appropriate etc (human readable size)
chovy, npm install filesize
13
function getBytes(string){
  return Buffer.byteLength(string, 'utf8')
}

3 Comments

This is just a copy of the accepted answer, put into a function.
Buffer.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.
The simplest and best answer
2

Alternatively, you can use TextEncoder

new TextEncoder().encode(str).length

Related question

Assume it's slower though

Comments

2

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.

Comments

1
console.log(Buffer.from('example..').length)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.