Remix.run Logo
▲ kragen 2 days ago

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

▲Someone a day 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.

▲kragen 18 hours ago | parent [-]

That sounds like strong evidence that V8 doesn't have an internal representation similar to UTF-8: it has one-byte encodings, and it has UTF-16, both of which are flat arrays of UTF-16 elements (and of code points, except for surrogates).

Although the article doesn't explain this, the one-byte encodings seem to be limited to Latin-1 (though not merely ASCII), in which the values of the bytes are literally the Unicode codepoints. Here we see that ASCII strings and Latin-1 strings are ONE_BYTE_INTERNALIZED_STRING_TYPE, but Greek and Latin-2 strings are INTERNALIZED_STRING_TYPE:

    : ~; node --allow-natives-syntax
    Welcome to Node.js v18.19.0.
    Type ".help" for more information.
    > %DebugPrint("hello")
    DebugPrint: 0x32c6da5c48f9: [String] in OldSpace: #hello
    0xee8766c1369: [Map] in ReadOnlySpace
     - type: ONE_BYTE_INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'hello'
    > %DebugPrint("dénouement")
    DebugPrint: 0x19b3181b5941: [String] in OldSpace: #d\xe9nouement
    0xee8766c1369: [Map] in ReadOnlySpace
     - type: ONE_BYTE_INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'dénouement'
    > %DebugPrint("dénouement∫")
    DebugPrint: 0x19b3181b7f91: [String] in OldSpace: u#d\xe9nouement\u222b
    0xee8766c2e69: [Map] in ReadOnlySpace
     - type: INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'dénouement∫'
    > %DebugPrint("κοκαλος")
    DebugPrint: 0x268799d03a79: [String] in OldSpace: u#\u03ba\u03bf\u03ba\u03b1\u03bb\u03bf\u03c2
    0xee8766c2e69: [Map] in ReadOnlySpace
     - type: INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'κοκαλος'
    > %DebugPrint("ł")
    DebugPrint: 0x268799d08681: [String] in OldSpace: u#\u0142
    0xee8766c2e69: [Map] in ReadOnlySpace
     - type: INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'ł'