3

I understand that it must at least be 1 byte to be memory addressable (see here), but I don't understand why a string character is 2 bytes (I'm aware that a character is two bytes because that is the space required to store a Unicode character), but a boolean is allocated 4 bytes. I've read the docs and am still unclear why this is the case.

Also, a Google search for System.Boolean size told me that it's to do with memory alignment. It's faster to push around a four-byte Int32 than it is to work with individual bytes/bits. If that is the case though, then why would a single string char still only be represented with 2 bytes?

Any ideas?

Jason Roell
  • 6,679
  • 4
  • 21
  • 28
  • @RichS read that, but it still doesn't make clear why a string char would be 2 bytes and boolean would be 4. – Jason Roell Apr 16 '19 at 18:56
  • char in javascript is `UCS-2` – Daniel A. White Apr 16 '19 at 18:57
  • 2 bytes are required for Unicode. – Reactgular Apr 16 '19 at 18:57
  • @DanielA.White I'm aware and that makes sense to me, but a boolean could be represented with 1 byte. That still doesn't tell me why it is 4 bytes. – Jason Roell Apr 16 '19 at 18:58
  • @cgTag I'm aware and that makes sense to me, but a boolean could be represented with 1 byte. That still doesn't tell me why it is 4 bytes. – Jason Roell Apr 16 '19 at 18:59
  • Where does it say a boolean is 4 bytes? variables are much larger than 4 bytes in JavaScript. There's no such thing as an array of boolean (arrays are not typed in JavaScript except for typedarray of which there is no boolean type). As for string it's left over from the 90s when people though 16bits was enough for all unicode. Of course that assumption was wrong and any language that used 16bits for string characters is suffering the consequenses. – gman Apr 16 '19 at 19:00
  • the string likely packs them in – Daniel A. White Apr 16 '19 at 19:01
  • @gman boolean is a primitive type in Javascript and v8 allocates 4 bytes for a boolean. – Jason Roell Apr 16 '19 at 19:01
  • 2
    That's a V8 choice. It's got nothing to do with JavaScript. It's up to the JavaScript engine how to represent booleans. It's not defined by the JavaScript spec. You should change your question to why does V8 use 4 bytes for a boolean – gman Apr 16 '19 at 19:02
  • 3
    I think [this answer](https://stackoverflow.com/questions/32733314/for-v8-how-to-indicate-and-store-primitive-type-valuenull-undefined-boolean-i) answers your question. –  Apr 16 '19 at 19:03
  • @gman Perhaps I'm mistaken, but my reading is that's exactly the question being asked here: why does V8 allocate 4 bytes for representing a boolean? – apsillers Apr 16 '19 at 19:03
  • @gman well of course. This is obvious, however, v8 is the most popular javascript engine and I haven't seen boolean allocated as anything less in any other engine. I could rephrase the question to ask why v8 in particular does this though I suppose. – Jason Roell Apr 16 '19 at 19:04
  • I answered why string used 16bits. You already answered yourself why boolean uses 4. Because of alignment – gman Apr 16 '19 at 19:05
  • 2
    @Amy thank you. I think that does answer my question! – Jason Roell Apr 16 '19 at 19:05
  • A "*single string char*" is not a JavaScript value. It's part of a much larger string. – Bergi Apr 16 '19 at 19:41

0 Answers0