In a Firefox addon I am caching lengthy strings to disk. I would like to be able to give users some idea of how much disk space in bytes these strings are taking up.
I understand that Javascript stores strings as UTF-16. If a UTF-8 string is saved in a variable, it is converted to UTF-16. So UTF-8 methods of determining string size will not do here.
From this reference:
It states that the value of string.length
is actually the number of UTF-16 code units, and not the number of characters.
From this I infer that the disk space in bytes would simply be string.length * 2
. I am looking for confirmation as to whether my assumption is correct.
EDIT:
(Several edits made to the title and original text. Also, the following:)
It was suggested that this is a duplicate of How many bytes in a JavaScript string?. However this does not address my question, as it refers to methods of getting string size of UTF-8 strings, however Javascript converts UTF-8 strings to UTF-16 when it stores them. For example a UTF-8 character that takes up 3 bytes may only use 2 bytes (1 UTF-16 code unit) when converted to UTF-16.