The V8 JavaScript Engine
Whether you've explored MEAN stack development or simply used Google Chrome, you've experienced the power of Google's V8 engine. In a nutshell, V8 is JavaScript engine that runs JavaScript code. While it's most popular application is Google Chrome, the V8 engine also powers popular dev tools like Node.js, MongoDb, and CouchBase. In this article, we explore the details behind what the V8 engine is and how it's used today to power our favorite dev environments.
What is a JavaScript Engine?
A JavaScript engine is simply an engine that interprets and runs JavaScript code. Unlike a compiled language like Java or Swift, JavaScript is an interpreted language. Rather than compiling source code down to machine code that is then executed, JavaScript is instead "interpreted" or read by the browser (or engine) at runtime. In this sense, JavaScript relies on a runtime environment to execute. This is where JavaScript engines come into play.
The Birth of V8
To understand why the V8 engine has become so popular, it's worth taking a look at the evolution of JavaScript engines. The first engine was developed by Brendan Eich (the father of JavaScript) at Netscape in 1995. This engine was nicknamed SpiderMonkey and was written entirely in C++. Shortly after, Rhino was developed by Norris Boyd as a Java implementation of the JavaScript engine.
It wasn't until the advent of AJAX in 2008/2009 when things started to heat up. As more developers began using JavaScript frameworks with Ajax to create dynamic front-end apps, tech giants raced to deliver something that would drastically improve JavaScript performance, especially in the browser.
Thus Google's V8 engine was born. In order to increase the performance of JavaScript execution in a browser, V8 was designed to translate JavaScript code into machine code (rather than relying on an interpreter). By using a just-in-time compiler, V8 was able to surpass other interpreters with superior performance without producing bytecode or any intermediate code.
A Closer Look at V8's Magic
So how did Google achieve this? How were they able to create an engine that runs JavaScript code through dynamic translation rather than interpretation?
While the details surrounding the development of the engine are beyond the scope of this article, things like garbage collection and inline caching are worth exploring. These concepts along with hidden classes are what largely contribute to V8's performance advantage.
Hidden Classes
Unlike other engines, V8 creates hidden classes at runtime. This may sound expensive, but hidden classes allow V8 to have internal representations of data types and improves property access time. By creating classes for internal referencing, V8 largely addresses JavaScripts' inherent problems (dynamically typed and prototype-based in nature).
Conclusion
It's no wonder the V8 engine has been applied to everything from database engines (MongoDb) to web servers (Node/Express). By understanding how the V8 engine works, developers can better organize code to leverage things like hidden classes and just-in-time compilers that V8 relies on to excel with performance.