Logo
My Blog
Blog

Timeline

Blog

Facts about Javascript – History & JS Engines

One of my mostly beloved languages is Javascript. As I often say, Java was my first love, but my eternal one is JS. Thousands of developers work with it nowadays and it’s being used on any kind of applications. But do you actually know the story behind Javacript? Have you ever wondered where is it being executed?

How was Javascript created?

So back in 1993, the Mosaic browser of National Center for Supercomputing Applications (NCSA) was one of the first popular web browsers. A year later, Netscape Communications created the proprietary web browser, Netscape Navigator. Several original Mosaic authors worked on Navigator.

In 1995, Netscape Communications hired Brendan Eich with the promise of letting him implement Scheme (a Lisp dialect) in the browser. Before this happened, Netscape got in touch with Sun Microsystems (now Oracle) to include Java in the Navigator browser. Due to the popularity and easy programming of Java, Netscape decided that a scripting language had to have a syntax similar to that of Java. This ruled out adopting existing languages such as Python, Tool Command Language (TCL), or Scheme.
Java VS Javascript

Eich wrote the initial prototype in just 10 days(!), in May 1995. JavaScript’s first code name was Mocha, coined by Marc Andreessen. Netscape later changed it to LiveScript, for trademark reasons. In early December 1995, Sun licensed the trademark Java to Netscape. The language was renamed to its final name, JavaScript.

Where is Javascript being executed?

Javascript needs a Javascript Engine to be executed. A Javascript engine is a program or interpreter which executes Javascript code. A Javascript engine may be a traditional interpreter, or it may utilise just-in-time compilation to bytecode in some manner. Although there are several uses for a Javascript engine, nowadays it is most commonly used in browsers.

The first JavaScript engine was created by (guess who) Brendan Eich for the Netscape Navigator web browser. The engine, code named SpiderMonkey, was implemented in C++. The Rhino engine, created primarily by Norris Boyd (also at Netscape) is another JavaScript engine implementation, in Java.

Browsers
Other JS engine implementations include Apple Safari’s Nitro, Google Chrome’s V8 and Mozilla’s TraceMonkey. By far the most common host environment for Javascript is a web browser. Web browsers typically use the public application programming interface (API) to create “host objects” responsible for reflecting the Document Object Model (DOM) into JavaScript.

Each Javascript engine implements a version of ECMAScript, of which Javascript is a dialect. As ECMAScript evolves, so do JavaScript engines. The reason there are so many different engines is that each one is designed to work with a different web browser, a headless browser, or a runtime like Node.js.

The V8 Engine

The most popular browser of our time is Chrome. Let’s have a closer look on its own JS engine, V8. V8 was first designed to increase the performance of the JavaScript execution inside web browsers. In order to obtain speed, V8 translates JavaScript code into more efficient machine code (set of instructions executed directly by a computer’s central processing unit) instead of using an interpreter. It compiles JavaScript code into machine code at execution by implementing a JIT (Just-In-Time) compiler, like other JavaScript engines such as SpiderMonkey or Rhino are doing. The main difference with V8 is that it doesn’t produce bytecode or any intermediate code.
Google's V8 Engine

The V8 engine, written in C++, compiles and executes JavaScript source code, handles memory allocation, and garbage collects leftovers. Its design consists of two compilers that compile source code directly into machine code:
1) Full-codegen: a fast compiler that produces unoptimized code
2) Crankshaft: a slower compiler that produces fast, optimised code.

If Crankshaft determines that the unoptimised code generated by Full-codegen is in need of optimisation, it replaces it, a process known as ‘crankshafting’. In general, the goal of a V8’s parsing and execution process is to generate the most optimised code in the shortest possible time.

Why even bother?

The evolution of those engines happens in parallel to our quest to evolve our web applications and make them as performant as possible. To track this evolution, you can see how various engines perform in benchmarking graphs such as those produced on arewefastyet.com. It’s interesting, for example, to compare Chrome’s performance when powered by V8 versus a non-Crankshafted engine.

In fact you will never need to get into the process of diving into the (really low-level) source code of a Javascript Engine, but a few insights regarding the execution of the code that you write are always helpful. Javascript Engines are the foundation of the applications that we build and let’s keep in mind that we all depend on them!

Leave A Comment