The V8 JavaScript Engine and Google Chrome for Linux

Page content

Have you ever used GMail? How about Google Docs? These Google mainstays, plus countless others from within and beyond Google, collectively illustrate the power of JavaScript. No longer just for client-side eye candy, JavaScript is the “JA” in AJAX (Asynchronous JavaScript and XML) - a powerful framework for developing and delivering Web applications. Google is so heavily vested in AJAX that it offers the Google Web Toolkit (GWT) that, somewhat curiously, allows Java applications to be translated into AJAX.

Given this commitment, it is not surprising then for a Web browser from Google to place a significant emphasis on JavaScript. And that’s exactly what V8 does. Because there are other JavaScript engines available, including WebKit’s, it is important to understand from the outset Google’s rationale for taking this on themselves.

Design Elements for V8

In developing V8, Google took a number of elements into consideration in the design phase:

  • Fast property access - JavaScript is an object-based language. (And though that is different from an object-oriented language, we will defer elaboration of that distinction to another time.) Not too surprisingly, objects in JavaScript have properties. And as a dynamic programming language, JavaScript allows properties to be added-to or deleted-from objects in real time, with a dictionary-like, in-memory data structure keeping track of the object-property relationships. Therefore, in most JavaScript engines, dynamic lookups are needed to resolve a property’s location in memory. Through the introduction of an approach based on hidden classes, the V8 JavaScript engine is able to execute lookups using fixed offsets. This approach also allows for use of inline caching, and ultimately achieves access efficiencies experienced in other languages such as Java.
  • Dynamic machine code generation - In part, platform independence is responsible for the popularity of languages like JavaScript (and Java). This independence is achieved through the use of bytecode that needs to be interpreted on execution. Of course, there is a penalty associated with the need to interpret bytecode that is not architecture optimized. Therefore, the outcome of a V8 compilation is machine code. But V8 doesn’t stop there. Through the use of a patching mechanism, that actually modifies the machine code in real time, V8 introduces a dynamic aspect that is wholly aligned with the hidden-class approach used for property access. Ultimately, this means that V8 is extremely efficient for object re-use.
  • Efficient garbage collection - Object-based languages like JavaScript need to periodically reclaim memory. Known as garbage collection, this process reclaims for other use, memory freed by objects that are no longer required. V8’s approach is to temporarily halt code execution and reclaim memory associated with some of the dereferenced objects. This approach of compromise impacts execution (overall) minimally. Throughout the garbage collection process, V8 keeps close tabs on all objects and pointers to ensure appropriate actions are taken - thus avoiding, for example, memory leaks.

Collectively then, V8 is designed from the outset to execute large JavaScript applications effectively and efficiently.

V8 and Google Chrome

Subject to the above design considerations, the V8 JavaScript engine has been implemented in C++. Because V8 is available under the BSD Open Source license, the specifics of the implementation are accessible at the source-code level.

V8 implements standardized EcmaScript. This is not quite JavaScript, as EcmaScript is the standardized language specification inspired by JavaScript and JScript. In existence since 1961, Ecma International is the standards body for EcmaScript.

V8 can be run in a stand-alone mode or embedded into any C++ application - like Google Chrome. Although V8 and Google Chrome are separate and distinct, they are bundled together, built together (please see “Google Chrome for Linux: Building from Source”), and unit-tested together (please see “Google Chrome for Linux: Testing and Contributing”). V8 has a lot to offer in the context of Google Chrome for Linux … and beyond.

This post is part of the series: Google Chrome for Linux

Google Chrome is shaking up the status quo for Web browsers. This series explores and expounds Chrome as it evolves for the Linux platform.

  1. Google Chrome for Linux: Building from Source
  2. Google Chrome for Linux: Testing and Contributing
  3. Google Chrome for Linux: The WebKit Web Browser Engine
  4. Google Chrome for Linux: The V8 JavaScript Engine
  5. Google Chrome for Linux: Why V8 Bests WebKit’s JavaScript Engine
  6. Google Chrome for Linux: Android Availability