Chrome V8 executes JavaScript code. Node.js is built on top of Chrome V8 and is a widely used runtime environment for serverless JavaScript functions. However, there are advantages to running functions directly on V8.
After reading this article you will be able to:
Copy article link
Chrome V8 is a JavaScript engine, which means that it executes JavaScript code. Originally, JavaScript was written to be executed by web browsers. Chrome V8, or just V8, can execute JavaScript code either within or outside of a browser, which makes server-side scripting possible.
Like a V8 (eight-cylinder) car engine, Chrome V8 is fast and powerful. V8 translates JavaScript code directly into machine code* so that computers can actually understand it, then it executes the translated, or compiled, code. V8 optimizes JavaScript execution as well.
*Machine code is a language that CPUs can understand. It is purely digital, meaning made up of digits.
Compiling is a process that translates code from one programming language into another. Typically a compiler translates the code from a higher level of abstraction to a lower level of abstraction from human-usable languages like JavaScript into machine-readable code.
Chrome V8 performs what is called just-in-time compilation. Instead of compiling JavaScript in advance, it compiles the code at the same time that it is executed.
A 'sandbox' is an environment for executing software that is isolated and partitioned off from other environments, even those on the same machine.
Sandboxing is a key feature of Chrome V8. Each process is sandboxed, which ensures that JavaScript functions run separately on it and the execution of one piece of code does not affect any other piece of code. (Unlike many sandboxing enterprise products, which open and run executable files within isolated virtual machines, V8 sandboxing does not slow performance.)
Node.js is a runtime environment* for executing JavaScript code, and it is built on the Chrome V8 engine. It is asynchronous, which means that it does not have to wait for one process to complete before starting another one. Like V8, Node.js is free and open-source. Unlike V8, it does not have built-in sandboxing.
(Those familiar with JavaScript will note the '.js' file extension; however, this is purely an aesthetic choice for the runtime environment's name to indicate its association with JavaScript and does not mean that Node.js is a JavaScript file.)
*A runtime environment is the software environment in which code is executed.
Serverless functions need a way to execute when they are triggered. Several serverless computing vendors offer Node.js as a runtime for serverless JavaScript functions (other runtimes are used for other languages).
Cloudflare Workers, however, run directly on V8. There are a few reasons for this. One reason is speed of execution for functions that have not been used recently. Cold starts are an issue in serverless computing, but running functions on V8 means that the functions can be 'spun up' and executed, typically, within 5 milliseconds or less. (Node.js has more overhead and usually takes a few milliseconds longer.) Another reason is that V8 sandboxes JavaScript functions automatically, which increases security.