4 ms·
Aren't JavaScript strings defined to be UTF-16? So parts of every browser will use UTF-16, forever!
by csande17 6y ago
Aren't JavaScript strings defined to be UTF-16? So parts of every browser will use UTF-16, forever!
- chungy 6y agoUCS-2, actually, but for practical purposes, everyone treats them as UTF-16 as long as they are also valid UTF-16.
- chrismorgan 6y agoI’d quibble over that characterisation. JavaScript uses neither UCS-2 nor UTF-16. Rather, its strings are a sequence of 16-bit Unicode code points, with surrogates handled according to UTF-16 rules, saving that unmatched surrogates are permitted to remain (that is, JavaScript strings aren’t necessarily valid Unicode). There is no requirement whatsoever that the browser actually use 16-bit code units to represent strings. This is what Simon Sapin achieved for Servo with the WTF-8 encoding, which extends UTF-8 to allow surrogates, including unmatched surrogates: it makes it able to represent these strings with 8-bit code units, commonly halving memory usage and improving the speed of various sorts of linear processing (though at the cost of random access speed which becomes O(n)).