String length in bytes in JavaScript

Alexander Gladysh picture Alexander Gladysh · Apr 1, 2011 · Viewed 115.5k times · Source

In my JavaScript code I need to compose a message to server in this format:

<size in bytes>CRLF
<data>CRLF

Example:

3
foo

The data may contain unicode characters. I need to send them as UTF-8.

I'm looking for the most cross-browser way to calculate the length of the string in bytes in JavaScript.

I've tried this to compose my payload:

return unescape(encodeURIComponent(str)).length + "\n" + str + "\n"

But it does not give me accurate results for the older browsers (or, maybe the strings in those browsers in UTF-16?).

Any clues?

Update:

Example: length in bytes of the string ЭЭХ! Naïve? in UTF-8 is 15 bytes, but some browsers report 23 bytes instead.

Answer

Riccardo Galli picture Riccardo Galli · Dec 17, 2015

Years passed and nowadays you can do it natively

(new TextEncoder().encode('foo')).length

Note that it's not supported yet by IE (or Edge) (you may use a polyfill for that).

MDN documentation

Standard specifications