Byte size of String in JavaScript

I am trying to understand memory used by Strings & Arrays. As per this helpful question: How many bytes in a JavaScript string?

Blob is a great way of checking byte size of Strings:
new Blob(['a']).size -> 1 byte

But Strings are encoded UTF-16 in JavaScript which uses minimum of 2 bytes. How does Blob return 1?

>Solution :

Blob uses UTF-8 to represent strings.
The minimum byte size for UTF-8 is 1 and character 'a' can be represented in UTF-8 using a single byte. A two-byte UTF-8 character ('Ђ' for example) returns 2, and something even longer like complex emoji ('😃') returns 4.

Leave a Reply