V8
Tags: javascript, webdev, opensource
Published: 2021-10-25 05:22:53

V8 is incredibly important to the web – and no, I'm not talking about the juice or the car engine.  I'm talking about Google's JavaScript Engine V8 that launched with the release of the Google Chrome Browser on September 2, 2008 as a part of the open source Chromium Project.  Before Chrome's release, JavaScript (and Jscript) performed slowly and would often bog down websites' responsiveness.  I, and many others, would block JavaScript, using a Firefox add-on called NoScript.  As seen in the image below, it was originally released for a much older version of Firefox.  
![image of NoScript use](https://addons.cdn.mozilla.net/user-media/previews/full/1/1060.png)

Google had already created JavaScript intensive web applications like Gmail and Maps and recognized that JavaScript needed to be faster in order to improve their web apps, and decided to be the change they wanted to see.  When Chrome launched in 2008, it used WebKit as its rendering engine, and a brand new JavaScript engine called V8.  WebKit was originally a fork of KHTML used in KDE's Konqueror browser, and remains the rendering engine for Apple's Safari, while Google forked WebKit in 2013 to create Blink.  Mozilla Firefox uses Gecko as its rendering engine and SpiderMonkey as its JavaScript Engine.  Microsoft's Internet Explorer and early versions of Edge used Trident as a rendering engine, and Chakra as its JavaScript and Jscript engine (two different versions with the same name – definitely not confusing).  Apple's Safari's rendering engine is still Webkit, as mentioned above, and its JavaScript engine is JavaScriptCore.  When Chrome launched in 2008, it had the best JavaScript performance out of all the browsers due to its V8 engine.
![image of many browser logos](http://stritar.net/Upload/Images/BrowserIcons.jpg)

The V8 engine launched with several notable improvements.  The most obvious of these is a shift from an interpreter to a JIT Compiler.  Interpreters read, parse, and execute code one line at a time.  This means that changes to the source code and quickly integrated into the application – Python and Ruby are interpreted languages.  This is in contrast to compiled languages like C/C++ and Java, which reads all of the code and converts it to a lower level language, often with optimizations.  Compiled code often runs faster, but it comes with the drawback of needing to be compiled every time the source code is changed, and this can take a significant amount of time as the source code grows in size.  A JIT Compiler tries to take the best of both worlds.  It does not compile code ahead of time, but rather as each line is read, or several lines at once.  If the JIT Compiler notices that certain code is reused several times throughout the program, it will save the already compiled code and call on that pre-compiled code when necessary.  If a certain code block is called even more times, an optimizing compiler will attempt to optimize even further.

Another reason for Chrome's excellent JavaScript performance at launch was due to its multithreaded approach.  Chrome keeps a separate thread for UI and I/O, and then dispatches as many threads as needed for executing JavaScript.  In addition, each tab's threads are sandboxed from other tabs.  This improves performance and stability by not letting buggy or lagging sites affect other parts of the browser, and takes advantage of the increasingly multithreaded nature of CPUs.  By 2008, AMD and Intel had already released dual and quad core CPUs for consumers.

With the success of V8 and its influence on other JavaScript engines, people began to use JavaScript as a dependable and flexible programming language for larger projects.  People began wanting to use JavaScript for desktop applications, and Chromium and V8 were brought out of the browser through Node.js.  Node allows JavaScript applications to run on the desktop or through the command line.  Node doesn't have access to browser exclusive features like window or document, but offers filesystem reading and writing.  There are also forks of Node that use SpiderMonkey or JavaScriptCore instead of V8.  In order to facilitate creating desktop JavaScript applications, the framework Electron.js was created in 2013 that uses Chromium and V8 to create cross-platform applications that use the familiar web app development process.  Many apps we use every day are built with Electron, such as VS Code, Slack, Discord, Skype, and OBS.
![javascript and electron](https://jlord.us/essential-electron/imgs/components.png)

The V8 engine ushered in a new age of JavaScript performance, and several competing JavaScript engines improved their performance by implementing their own JIT Compiler.  For instance, Mozilla's JIT Compiler is called IonMonkey.  Combined with ever evolving JavaScript features, web apps are able to do more than ever.  Chromium and V8 were so successful that many other browsers are now based on the Chromium project, such as Opera, Edge, and Brave, and are responsible for over 70% of all web browser use.

Sources  
* https://v8.dev/blog/
* https://blog.chromium.org/
* https://chromium.googlesource.com/v8/v8.git/
* https://docs.google.com/document/d/1aitSOucL0VHZa9Z2vbRJSyAIsAz24kX8LFByQ5xQnUg/edit
* https://wiki.mozilla.org/IonMonkey
* https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/
* https://nodejs.dev/learn/the-v8-javascript-engine
* https://chromium.googlesource.com/chromium/src/+/lkgr/docs/threading_and_tasks.md