- Published on
Memory size of Javascript Boolean
This post is the result of research based on my implementation of the Run-length Encoding (RLE) algorithm for JavaScript. I found an interesting point about the size of boolean values in JavaScript. I use RLE to reduce the size of data in data labeling tool for Machine Learning models, but a boolean value doesn't take up just 1 byte as I had thought. The answer turned out to lie in the workings of the V8 engine.
Overview
Javascript in the browser operates at a level above Javascript Engine. Here is a list of popular projects that implement a JavaScript engine:
- V8 is an open-source engine developed by Google in C++
- Rhino is an open-source engine developed entirely in Java and managed by the Mozilla Foundation
- JavaScriptCore is an open-source engine developed by Apple for Safari
- SpiderMonkey is an open-source engine by Netscape and currently maintained by Mozilla Foundation
- Chakra is an open-source developed by Microsoft for Microsoft Edge
These engines follow the ECMAScript standard, which defines the scripting specification.
In this post, we will use V8 Engine by Google. The V8 engine is written in C++ and is used in Google Chrome, Node.js, and other platforms. It implements the ECMAScript standard as specified in ECMA-262. More detailed documentation on the V8 engine is available at V8 Docs.
How the V8 Engine works
The main scheme of compiling JavaScript code to machine code:
High-level languages (like a JavaScript) are very abstracted from machine language. Your JavaScript code undergoes many transformations before it is executed as machine code. Let's walk through this process step by step:
1. You write your JavaScript code in an IDE.
2. V8 engines parses provided 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:
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 happens first 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. The bytecode goes through a lot of optimization and transformations and is finally compiled to machine code. To find out more, read this article.
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 is aligned in such a way that the least significant bit or three bits 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 it 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)].