What is the difference between JavaScript engines and runtimes?

The runtime environment (from the English runtime – runtime environment) and engines are often mistakenly called the same thing.

You've likely heard the terms “JavaScript engine” and “JavaScript runtime” used interchangeably to mean “a program that runs JavaScript.” They are often mixed up by referring to V8, Node.js or some other combination of similar programs. However, there is a significant difference between the JavaScript engine and the JavaScript runtime in terms of their scope and functionality. Understanding this difference is key to a good understanding of the JavaScript language as a whole.

Before discussing what an engine and runtime are, it's worth defining a few terms that are often used in conjunction with both an engine and a runtime: ECMAScript and JavaScript.

What is ECMAScript?

In 1996, Netscape and Sun Microsystems approached Ecma International, a non-profit standards organization, to standardize JavaScript. This collaboration resulted in the 1997 release of ECMA-262, a specification that defines how a JavaScript implementation should operate. Due to Sun's reluctance to transfer JavaScript trademark the specification existed under a different name, so ECMA-262 was renamed the “ECMAScript language specification”. Thus, ECMAScript is the name of the language declared in the ECMA-262 specification.

ECMAScript defines the core functionality of JavaScript, which must be implemented accordingly regardless of where it is embedded (the program that embeds the implementation is called a host). Even at this early stage, Netscape already intended to use JavaScript not only in the browser, but also on the server, and ECMAScript was to be the basis for both implementations. ECMAScript itself does not contain any web-centric functionality, be it input or output information, such functionality must be provided by the host. This means that ECMAScript contains, for example, standard global classes such as Object, Array And Promisebut does not contain HTMLElement, setTimeout or fetch().

What is JavaScript?

Although the term “JavaScript” has no formal definition, it is commonly understood as an extension of the ECMAScript language. This means that JavaScript implements ECMAScript in addition to other specifications and functionality. In its early form, JavaScript was seen as ECMAScript + APIs based on web technologies, such as the Document Object Model (DOM) and APIs based on browser technologies, such as the History API. global history object). Today, JavaScript is considered to be any combination of ECMAScript + any host-provided API, including web-centric APIs for browsers and server-centric APIs for hosts such as Node.js.

What is a JavaScript engine?

What are generally referred to as JavaScript engines can more accurately be called ECMAScript engines because they implement the ECMA-262 specification without almost any additional functionality. The JavaScript engine is designed to be embedded on the host, which in turn defines additional functionality for input and output. The most famous JavaScript engines are the following:

  • V8 – was created as a JavaScript engine for the Chromium project, now also used in Node.js and Deno. Since Edge and Opera are based on Chromium, V8 can rightfully be called the most commonly used JavaScript engine.

  • SpiderMonkey – JavaScript engine for Firefox.

  • JavaScriptCore – was created as a JavaScript engine for Safari, both on MacOS and iOS, and is also used in Bun.

Since JavaScript engines implement only ECMAScript and must be extensible by the host, they can be used in a variety of runtime environments.

What is the JavaScript runtime?

The JavaScript runtime is an ECMAScript host, i.e. a program that embeds a JavaScript engine. So Chrome, Firefox, Edge, Safari, Node.js, Deno and Bun are all JavaScript runtimes because they embed the JavaScript engine and define additional functionality that is accessible through JavaScript. So web browsers use the DOM and other web APIs, while the server runtime accesses the file system.

There are no rules regarding what additional functionality the JavaScript runtime must have. Runtime developers are free to decide this issue themselves. This is why Node.js, Deno and Bun all handle the file system differently and why Deno decided to favor a web API like fetch()while Node.js initially decided to implement their own HTTP client (Node.js has since also adapted fetch()). But it's not just the JavaScript API that makes JavaScript runtimes unique, it's also how they use the JavaScript engine.

An event loop is a process that allows the runtime to switch between running JavaScript and performing other tasks. This process is not defined in ECMA-262 and is not implemented in any way in any JavaScript engine. The implementation of your own event loop is left to the JavaScript runtime. Web browsers have their own version of the event loop, defined in HTML specificationswhile server-side runtimes like Node.js have your version of this process. The event loop is not required by the JavaScript runtime, but it is present in all generic JavaScript runtimes.

Summarizing

JavaScript engines and runtimes are related, but they are nonetheless different. The JavaScript engine implements ECMAScript according to the ECMA-262 standard. This standard defines the core functionality of JavaScript without any implementation of input and output. The JavaScript runtime is an ECMAScript host that embeds the JavaScript engine and augments it with various input and output functionality along with whatever else is needed for the runtime itself. Additional functionality may include DOM in a web browser or file system access in server runtime environments. The runtimes themselves do not have any obligation to follow other standards and are free to define their own required APIs, which is why Node.js, Deno and Bun have different APIs for working with the file system.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *