JavaScript字符串中有多少字节?

问题描述:

我有一个javascript字符串,当从服务器以UTF-8发送时大约是500K。如何在JavaScript中告诉它的大小?

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?

我知道JavaScript使用UCS-2,所以这意味着每个字符2个字节。但是,它是否依赖于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?

字符串根据 ECMA-262第3版规范,值不依赖于实现,每个字符代表一个单个16位单位的UTF-16文本

String values are not implementation dependent, according the ECMA-262 3rd Edition Specification, each character represents a single 16-bit unit of UTF-16 text:


4.3.16字符串值

4.3.16 String Value

字符串值是String类型的成员,是
有限有序序列,零或
更多16位无符号整数值。

A string value is a member of the type String and is a finite ordered sequence of zero or more 16-bit unsigned integer values.

注意虽然每个值通常为
代表一个16位单位的
UTF-16文本,但该语言不是
的地方任何限制或要求
的值除了它们是
16位无符号整数。

NOTE Although each value usually represents a single 16-bit unit of UTF-16 text, the language does not place any restrictions or requirements on the values except that they be 16-bit unsigned integers.