Remix.run Logo
▲ Someone a day ago

> Javascript uses UTF-16 for strings

I don’t think the spec requires that. charAt indexes on UTF-16 code points, but I don’t remember ever seeing that it has to do that in O(1) time, so that doesn’t imply the implementation has to use UTF-16 internally.

Also, for modern JavaScript, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... says:

“Note: Avoid re-implementing the solutions above using charAt(). The detection of lone surrogates and their pairing is complex, and built-in APIs may be more performant as they directly use the internal representation of the string. Install a polyfill for the APIs mentioned above if necessary.”

Because of that, I would hope most modern implementations to at least have a variant internal representation that is similar to UTF-8.

▲kragen 19 hours ago | parent [-]

I wouldn't expect them to. Let us know if you find otherwise!

▲Someone 4 hours ago | parent [-]

https://iliazeus.lol/articles/js-string-optimizations-en/

“The ECMAScript standard defines a string as a sequence of 16-bit elements. But in a lot of cases, storing 2 bytes per character is a waste of memory. In fact, quite a lot of strings in most code will be limited to ASCII. That is why V8 has both one-byte and two-byte strings”

https://github.com/v8/v8/blob/main/src/objects/string.h confirms that.