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 of2 bytes
. How doesBlob
return 1? -
Why does the following program return
39800
instead of0
? I thought it would return 0 because the array is empty. What is the overhead with creating arrays?
const x = 200;
const y = 200;
const changes = []
for (let i=0;i<y;i++) {
const subItem = []
changes[i]=new Array(x).fill('');
}
console.log(new Blob(changes).size)
- Why does the following program return
79800
instead of40000
? Since a is only 1 byte in Blobs.
const x = 200;
const y = 200;
const changes = []
for (let i=0;i<y;i++) {
const subItem = []
changes[i]=new Array(x).fill('a');
}
console.log(new Blob(changes).size)
2
Answers
However, the
new Blob([data])
method also accepts arrays of "data", and if you pass a character as a single element in an array, it may not be interpreted as a string, but as a single byte. This can lead to the output you mentioned where new Blob([‘a’]).size returns 1 .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.