You have probably heard of the latest and greatest thing to be born in the Javascript universe. Yes it's Deno 🦕 Apart from the cute logo and a scrambled version of its big brother NODE, it is something that is being loved by the community and has attracted a lot of attention, which it deserves. The official Deno website already explains the concept in a nice and simple way which I would like to quote:
"Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust."
While many of the community folks have shared some interesting articles and examples on getting started with Deno, I would like to share the 'how things work inside of Deno' in simple terms. Just like understanding the basics of how JavaScript work under the hood or how Node works, it helps us grasp the concepts better, so knowing about the basic internal working concepts of Deno can help us understand it better and develop better programs with it. I would like to elaborate on the concepts using some Q & As.

What are the basic building blocks of Deno?

Deno is mainly made up of,
You can think of javascript/typescript as the frontend (unprivileged side) of Deno and Rust being the backend (privileged side). The Deno core API provides bindings to interact and communicate between the javascript and Rust world. JavaScript alone can't access the file system or set a timer for example. Whenever you write any code that does any such task in Deno, then javascript/typescript talks to Rust via the Deno core API to accomplish it.
What happens when we write some simple javascript code in Deno?
  1. function printToConsole() {
  2. console.log(
  3. 'This is my first Deno program, and I am pretty excited! Hello 🦕'
  4. );
  5. }
  6. printToConsole();
Since it is simple Javascript code, the code is fed to the V8 engine and prints the message to the console. Yeah nothing fancy, it's just like writing the same code in the browser console.
What happens when we write some typescript code in Deno?
  1. function printNameToConsole(name: string) {
  2. console.log(`Welcome ${name} to Deno World 🦕`);
  3. }
  4. printToConsole('Allen'); // Welcome Allen to Deno World 🦕
This time Deno hands it over to the typescript compiler to convert the typescript code into Javascript code and then it it transferred to the V8 engine. Deno uses V8 snapshots to speed up the typescript compilation process.
What happens when we write some Async code in Deno?
  1. function printNameToConsole(name: string) {
  2. console.log(`Welcome ${name} to Deno World 🦕`);
  3. }
  4. printToConsole('Rob');
  5. setTimeout(() => {
  6. printToConsole('John');
  7. }, 1000);
  8. printToConsole('Allen');
  9. //Welcome Rob to Deno World 🦕
  10. //Welcome Allen to Deno World 🦕
  11. //Welcome John to Deno World 🦕
When V8 sees the first printNameToConsole() statement, it prints the name, then it sees setTimeout which it identifies as a something which is outside of the javascript world. So it talks to Tokio via the rusty_v8 channel. It then goes to the next line and prints the third function to the console. Tokio meanwhile spins up something known as a thread pool to set a timer and executes the function in the background. Once the delay is completed, it communicates back the message to the V8 engine via the rusty_v8 channel which then prints the message to the console.
Deno also provides a neat API that can be called using Deno.metrics(). It provides stats from the Rust side of Deno about the information of the operations that took place.
Something like this,
console.table(Deno.metrics())
(index) Values
opsDispatched 3
opsDispatchedSync 2
opsDispatchedAsync 1
opsDispatchedAsyncUnref 0
opsCompleted 3
opsCompletedSync 2
opsCompletedAsync 1
opsCompletedAsyncUnref 0
bytesSentControl 73
bytesSentData 0
bytesReceived 375
This is a very simplified version of the data communication which happens in Deno. There are lots of other things to talk about Deno regarding its great features which I can probably share in another post with some more real-life examples.
Hope you enjoyed this post. Have a great one!