Tools like Node, Deno, and Bun permeate the modern web development world, yet very few understand how exactly these tools work. This article will attempt to dispel some of the mystery surrounding this area through an intuitive and not overly technical explanation.
The V8 Engine
Before we analyze the inner workings of these runtimes, let’s first look at the expected behavior. When you run a command such as node run.js or deno run run.ts the expected result is that our run.js file is executed and some action is done. This includes
- If there are any asynchronous tasks (future tasks, network calls, timers, etc…), the runtime needs to recognize these and handle them correctly, deferring the logic to the V8 engine at the right time and queuing tasks correctly.
Let’s tackle the last part of this first — asynchronous tasks. The way these runtimes handle these tasks is through a mechanism called the “event loop”, which is usually built into the runtime.
Now let’s handle the 1st and 2nd points. When you run deno run.ts a few things happen.
Next, any modules that you import as part of your code also have to be resolved and/or cached, which in the case of Deno are ES modules, and CommonJS modules for Node.
Finally, all this is fed into the V8 engine which exposes APIs for a lot of these things.
And that’s it! Well obviously there’s a lot more to all this. However, this covers the very basics and should allow you to look into the technicalities with more confidence.