Published on

Memory size of Javascript Boolean

Authors
Memory size of Javascript Boolean

This post is result of reasearch based on my implementation of Run-length Encoding (RLE) algorithm for JavaScript. I found interesting point about a size of boolean values in JavaScript. I use RLE for reduce the size of data in data labeling tool for machine learning models but boolean value doesn't take 1 byte as I thought. And answer turned out to be in the work of V8 Engine.

Overview

Javascript in the browser works a level above Javascript Engine. This is a list of popular projects that are implementing a JavaScript engine:

  • V8 - open source engine managed by the Mozilla Foundation
  • Rhino - open source engine developed entirely in Java and managed by the Mozilla Foundation
  • JavaScriptCore - open source engine developed by Apple for Safari
  • SpiderMonkey - open source engine by Netscape and currently maintained by Mozilla Foundation
  • Chakra - open source developed by Microsoft for Microsoft Edge

These engines follow the ECMAScript Standards. ECMAScript defines the standard for the scripting language.

In this post, we will use V8 Engine by Google. The V8 Engine is written in C++ and used in Google Chrome and Node.js, among others. It implements the ECMAScript Standart as specified in ECMA-262. More specific documentation of the V8 Engine is available at docs.

How the V8 Engine works

The main scheme of compilation JavaScript code to Machine Code:

Javascript to Machine Code scheme

High-level languages (like a JavaScript) are very abstracted from machine language. And your JavaScript code goes many transformations than will be executed in machine code. Let's walk through this process step by step:

1. Write your JavaScript code in IDE.

2. Parse JavaScript code to Abstract Syntax Tree (AST). You can explore on AST Explorer.

function test(x) {
	if (x > 10) {
		return x + 20;
	}

	return x + 10;
}

The parser will produce the following AST:

AST

Note that for visualization purposes, this is a simplified version of what the parser would produce. The actual AST is much more complex. The main idea is to get a feel for what would be the first thing that would happen to the source code before it gets executed.

3. V8’s Ignition bytecode compiler takes AST produced by the parser as input and produces a stream of bytecode (BytecodeArray) along with associated meta-data which enables the Ignition interpreter to execute the JavaScript source.

4. Bytecode going through a lot of optimization and transformations and finally compiles to machine code. To find out more, read this article, it will help you but we'll skip this step for now.

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

  5. https://blog.cloudflare.com/binary-ast/

  6. https://v8.dev/blog/background-compilation

  7. https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775

  8. https://blog.digilentinc.com/true-or-false-boolean-is-a-data-type/