Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many bytes in a JavaScript string?

I have a javascript string which is about 500K when being sent from the server in UTF-8. How can I tell its size in JavaScript?

I know that JavaScript uses UCS-2, so does that mean 2 bytes per character. However, does it depend on the JavaScript implementation? Or on the page encoding or maybe content-type?

like image 409
Paul Biggar Avatar asked Feb 08 '10 04:02

Paul Biggar


People also ask

How big can a JavaScript string be?

Max length of a JavaScript string According to ECMAScript specification a string cannot contain more than 9,007,199,254,740,991 (2⁵³-1) characters.

How do you calculate bytes of a string?

So a string size is 18 + (2 * number of characters) bytes. (In reality, another 2 bytes is sometimes used for packing to ensure 32-bit alignment, but I'll ignore that). 2 bytes is needed for each character, since . NET strings are UTF-16.

How much storage is a string?

An empty String takes 40 bytes—enough memory to fit 20 Java characters.


1 Answers

This function will return the byte size of any UTF-8 string you pass to it.

function byteCount(s) {     return encodeURI(s).split(/%..|./).length - 1; } 

Source

JavaScript engines are free to use UCS-2 or UTF-16 internally. Most engines that I know of use UTF-16, but whatever choice they made, it’s just an implementation detail that won’t affect the language’s characteristics.

The ECMAScript/JavaScript language itself, however, exposes characters according to UCS-2, not UTF-16.

Source

like image 162
Lauri Oherd Avatar answered Sep 19 '22 12:09

Lauri Oherd