skip to Main Content

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

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

  2. Why does the following program return 39800 instead of 0? 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)
  1. Why does the following program return 79800 instead of 40000? 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


  1. 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 .

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search