0

What would be smaller in memory size between:

[true,true,true,true,true,true,true,true,true,true]

and

new Int8Array(1,1,1,1,1,1,1,1,1,1) ?

Considering regular arrays can contain anything, each slot must have a size of 32 bits while the Int8Array would be 8 bit each. Is that right?

RainingChain
  • 7,397
  • 10
  • 36
  • 68

1 Answers1

1

Hm, that intuition makes sense to me, but testing with a million on Chrome, the array of bools does not blow up my memory nearly as much as the Int8Array, which takes far more space than might be expected (maybe a lot of scaffolding in each instance of Int8Array?).

Have you considered using bitmasks? How many boolean values do you need to store in each array? If no more than 32, you could go with a simple integer; otherwise, a combinations of UIntArray and bitmasks should do the trick, as per How do I create bit array in Javascript?.

There are also pre-made bit-array implementations out there, like https://github.com/madrobby/bitarray.js/blob/master/bitarray.js.

Community
  • 1
  • 1
Ozan Bellik
  • 483
  • 2
  • 6