What Is Deno? And how is it different from NodeJS?

Navyaa Sharma
CodeChef-VIT
Published in
4 min readJul 4, 2020

--

In this article I will be making you familiar with what Deno is and how is it different or similar to NodeJS. In the end I will talk about “Can Deno replace NodeJS?” Lets get started…

Deno is a runtime (not a programming language) built on V8, the Google runtime engine for JavaScript that can execute JavaScript and TypeScript. Deno needs to first compile TypeScript to JavaScript.

Let’s look at common features of Deno and Node:

  1. They’re both runtime environments for JavaScript
  2. They both run on any computer where you can run V8
  3. They both have ECMAScript standard support
  4. They’re both actively maintained

Setting up Deno

For setting up Deno in your system you need to do different steps in case of windows and MAC user. Get started here https://deno.land/

In case you are a Windows user you need to open “windows PowerShell” (not the command prompt) and enter the following command there.

iwr https://deno.land/x/install/install.ps1 -useb | iex

After this, you are good to go and you can start exploring Deno.

Deno Runtime APIs (Namespace APIs)

The are the core APIs or the core features that are built into Deno and which are available on a “Deno” object, which is globally available on any Deno code you are executing. There you get core modules for working with files , requests etc.

Apart from these Deno has a standard library (which is being maintained by the Deno team) and Third-Party modules which contain various useful modules for writing out code for applications.

Let's start writing our first Deno code and with this, we will get to see how Deno differs from NodeJS

This is our first code, nothing much fancy about. It will write the text given onto a file named “message.txt”.

Running our Deno code: Run this command in your terminal.

deno run app.ts

Oops… When you will execute this command you will see an error message coming up like this-

So here it is, we see our first difference-

All the scripts that are to be executed in Deno need user permissions like read, write access etc. Deno executes the code in a sandbox, which means that runtime has no access to the network, the file system, and the environment. Deno provides permission whitelist. For the file system, you can use --allow-read and --allow-write. You can use --allow-net flag to access host/url. For .env file, you can use --allow-env.

In node we did the same thing using “fs” module.

The most important of Deno’s features is its focus on security.

So we will rectify the error by giving write permissions and execute or app.ts file again.

deno run --allow-write app.ts

With this, you completed writing your first Deno code, and you will get to see a “message.txt” file getting created with the message written in it.

Second difference -

Another major difference that can be noticed from the above code snippet is- Deno embraces modern javascript features like promises, therefore here writeFile() doesn’t take a call back to let us know when it is done (as it did in NodeJS) instead here, we can call then() and catch() because writeFile() returns here a promise. So if writing on file is a success we can listen it with then().

Some of the other differences in NodeJs and Deno are-

  • Deno fixes the node_modules bloat. You don’t get a package.json file or a mandatory node_modules folder within your project. These are some of the benefits of Deno’s dependency handling mechanism. The main advantage I have seen is that all the dependency handling mechanism that operates transparently to the developer. If the developer needs to get involved explicitly, he can do so (using--reload the flag we can ask to force install dependencies. Similarly, you can define the dependency folder).
  • There are differences in their core implementation (Node uses C++ and Deno uses Rust), Event Loop &, etc.
  • When we start the application, Deno downloads all the imported modules and caches them. Once they are cached, Deno will not download them again until we specifically ask for it with the --reload flag.

So can Deno replace NodeJS?

Deno has a long way to go as it is not mature now to be used in large projects and industries. Ryan Dahl, creator of NodeJS and Deno stated the fact that “NodeJS is not going anywhere, it has reached a level of stability and maturity in the usage and industry”.

Deno’s features are quite appealing but Deno is in the development phase now. So we can expect new features coming up. Some claim that Deno is fast as compared to NodeJS.

Deno needs to first compile TypeScript to JavaScript (happens transparently); it will increase the cold start time. Therefore, it’s essential to keep this in mind if you make any performance comparisons.

So let’s come back to the actual performance comparison. When I look at the benchmarks available in Deno’s official site, I observed that the performance differences aren’t that significant. I feel we need more criteria before claiming that Deno is fast.

Enjoyed reading?

Stay tuned for more such blogs on Deno!
Till then keep reading.

What’s next?
Developing REST APIs in Deno

--

--