Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much RAM does each character in ECMAScript/JavaScript string consume?

The question is pretty simple: how much RAM in bytes does each character in an ECMAScript/JavaScript string consume?

I am going to guess two bytes, since the standard says they are stored as 16-bit unsigned integers?

Does this mean each character is always two bytes?

like image 451
Tower Avatar asked Aug 27 '11 20:08

Tower


1 Answers

Yes, I believe that is the case. The characters are probably stored as widestrings or UCS2-strings. They may be UTF-16, in which case they take up two Words (16 bit integers) per character for characters outside the BMP (Basic Multilingual Plane), but I believe these characters are not fully supported. Read This blog post about problems in the UTF16 implementation of ECMA.

Most modern languages store their strings with two byte characters. This way you have full support for all spoken languages. It costs a little extra memory, but that's peanuts for any modern computer with multiGig RAM. Storing the string in more compact UTF8 will cause processing to be more complex and slower. UTF8 is therefore mostly used for transportation only. ASCII supports only Latin alphabet without diacritics. ANSI is still limited and needs a specified code page to make sense.

Section 4.13.16 of ECMA-262 explicitly defines "String value" as a "primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integers". It suggests that programs use these 16-bit values as UTF-16 text, but it is legal simply to use a string to store any immutable array of unsigned shorts.

Note that character size isn't the only thing that makes up the string size. I don't know about the exact implementation (and it might differ), but strings tend to have a 0x00 terminator to make them compatible with PChars. And they probably have some header that contains the string size and maybe some refcounting and even encoding information. A string with one character can easily consume 10 bytes or more (yes, that's 80 bits).

like image 183
GolezTrol Avatar answered Oct 06 '22 00:10

GolezTrol