The Code that Runs Your JavaScript Code

The Code that Runs Your JavaScript Code

How often do you think about the environment that your code runs in, and the resources that are available to it when it runs? Understanding the ‘runtime’ environment can help us make better choices as JavaScript developers when writing code.

How often do you think about the environment that your code runs in, and the resources that are available to it when it runs? Understanding the ‘runtime’ environment can help us make better choices as JavaScript developers when writing code.

When you take a script in your editor of choice and you hop into your terminal and type node test.js, what is really happening to allow your code to execute? The runtime environment is spun up for you in a split second, defined primarily by the “V8 Engine” and a few additional constructs know together as the “JavaScript runtime”. It applies the collective wisdom of 20 years of the worlds finest engineers to find the absolute most efficient way to run your puny little script, eventually logging “hello world” inside your REPL loop.

Image for post

Synchronous Execution (complexity level 1)

To start examining the above image, let’s first look at a very simple example, where your script only contains synchronous function calls. In this situation, your script will only interface with the Callstack and Heap, parts of the V8 Engine. You can, for now, ignore all of the information on the right hand of that diagram.

The heap is responsible for saving the state of the functions and variables that you define in your script, and I will essentially ignore it in the following code examples this is so that I can better focus this talk on the topics of asynchronous programming and the event loop.

So let’s take the following script:

const bar = (arg) => { return arg }
const foo = (arg) => { return bar(arg) }
foo('function call')

The first thing that the runtime will do is break down your JS source code into chunks of executable subroutines, called stack frames. Stack frames are a concept commonly used in CS . The easiest way to think about a stack frame is as follows:

Each stack frame corresponds to a call to a function or procedure which has not yet terminated with a return. For each function call, a frame is created containing `function_`'s arguments and local variables. [Credit_](

The above script will generate 2 separate stack frames, the first contains foo’s argument and local variables. When foo calls bar, a second frame is created with the arguments and local variables of bar. Local variables here included the function bar. When bar returns, that stack frame is popped from the stack and the foo frame will return, finally clearing the stack. The call stack is a LIFO data structure, which means the last thing frame added to the stack will be the first on to be executed.

The environment feeds stack frames into the V8 Engine one by one for execution, which subsequently executes those stack frames, or hands them off to other APIs that are available in the runtime.

web-development javascript internet code programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

JavaScript Web Developer Bootcamp - Complete Guide to Become JavaScript Developer

In this Video we will learn these concept and there are almost 10+ Project in javaScript. One Video For becoming web developer. This video will teach you web development from scratch.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Web App Development Company | Web Application Development Service

Web development company in India & USA offers different types of web application development services with the best result oriented, clients all specifications, different technologies, etc with a cost-effective rate. Get a free quote.