DEV Community

Cover image for Memory size of Javascript Boolean
Nik Shevchenko
Nik Shevchenko

Posted on • Originally published at shevchenkonik.com

Memory size of Javascript Boolean

Introduction

Javascript in the browser works a level above Javascript Engine (V8, Rhino, JavaScriptCore, SpiderMonkey). These engines follow the ECMAScript Standards. ECMAScript defines the standard for the scripting language. [1]

In this post, we will use V8 Engine by Google. The V8 Engine:

  • The V8 Engine is written in C++ and used in Chrome & in Node.js, among others.
  • It implements the ECMAScript Standard as specified in ECMA-262. [1]

More specific documentation of the V8 Engine is available at docs.

The main scheme of compilation JavaScript code to Machine Code:

Javascript to Machine Code scheme

Each object takes a specific size in memory and C++ or ECMAScript specify on this size.

Boolean

Size: 4 bytes or 1 byte

A boolean is actually 1 byte. But alignment may cause 4 bytes to be used on a 32-bit platform or 8 bytes on a 64-bit platform. This old trick comes from the observation that allocated memory takes up at least 4 or 8 bytes, and are aligned in the way that the least significant bit or three will be zero.

In C++, the size of the type boolean is implementation-defined (expr.sizeof[p1]) and is usually equal to 1 (the size of the type char, and the smallest size a type can have), but is not required to be (expr.sizeof[fn77]): in particular, in Visual Studio up to version 4.2, it was 4. More information about C++ boolean values is available at docs[expr.sizeof(7.6.2.4)].

Resources

[1] –– https://www.ecma-international.org/publications/standards/Ecma-262.htm

[2] –– https://www.quora.com/In-C%2B%2B-what-is-the-size-of-type-bool/answer/Sergey-Zubkov-1?ch=10&share=2471829a&srid=lXWU

[3] –– https://www.freecodecamp.org/news/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964/

[4] –– https://stackoverflow.com/questions/32733314/in-v8-how-are-primitive-types-such-as-null-undefined-and-boolean-stored-in-me

Top comments (3)

Collapse
 
gruckion profile image
Stephen Rayner

Why did you write this article? What problem was you solving and why was this information important for that context?

Collapse
 
shevchenkonik profile image
Nik Shevchenko

Hey Stephen! I worked on front-end applications for processing (en.wikipedia.org/wiki/Image_segmen...) high-quality & resolution images (space telescope images for example). One important thing about stable work is to use as little data (between frontend & backend) as possible. This article is part of my R&D process..

Collapse
 
brianenno profile image
Brianenno

Thank you Nik, really usefull article!