Deno vs Node comparison. Similarities and differences

Contrary to popular belief, Node did not magically fall from the sky and become a success story overnight. The similarities between Node and Deno start with their creator — Ryan Dahl. Ryan Dahl even brought to light issues he wanted to address in this conference. I highly recommend you watch this video .

TLDR. What are the differences I’ll notice?

TYPESCRIPT!

One of the nicest changes with Deno is that it supports Typescript right off the bat, without much overhead. Here’s a sample “Hello World” written with Deno from their docs :

function capitalize(word: string): string {
  return word.charAt(0).toUpperCase() + word.slice(1);
}

function hello(name: string): string {
  return "Hello " + capitalize(name);
}

console.log(hello("john"));
console.log(hello("Sarah"));
console.log(hello("kai"));

A strong, independent package manager

One of the biggest changes compared to Node is the package.json, or the lack thereof. Deno does not have strong coupling with NPM modules (or anything similar to NPM). Instead, Deno uses import references:

import {
  add,
  multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js";

function totalCost(outbound: number, inbound: number, tax: number): number {
  return multiply(add(outbound, inbound), tax);
}

console.log(totalCost(19, 31, 1.2));
console.log(totalCost(45, 27, 1.15));

This means that you won’t have a node_modules folder. Modules are cached, and if you need to clear the cache, you need to run Deno with a --reload flag. Deno has a node compatibility layer , but it is still a work in progress. If you’re trying to port over a library import from node, you can also try using jspm.io, which will convert NPM modules to ES modules.

Security by default

Deno runs code in a sandbox and requires all permissions to be passed via flags : these include network, environment and file access. This is a stark contrast to Node which has direct access to all of these.

deno run --allow-all freedom.ts

Curious as to what the permissions are? Head over to the official docs (And don’t do ^ in your server code!).

In the beginning, there was nothing

Node didn’t support promises , which means that the APIs for async operations had to handle operations to take an error-first callback. Newer versions of Node support this, but backwards compatibility has made things messier under the hood. This isn’t the case with Deno, which supports top-level await, meaning you can use await in your main script without having to wrap it in an async function.

// NODE implemntation

const fs = require('fs');
fs.readFile('readme.txt', (err, data) => {
  if (err) {
    // Handle the error
  }
  // Otherwise handle the data
});


// Deno implemenation
try {
  const data = await Deno.readFile('readme.txt');
  // Handle the data
} catch (e) {
  // Handle the error
}

Better browser standard integration

window, addEventListener, and fetch are part of the runtime. There is even window.location support which resolves to the location of the running file.

Deno vs Node — what are the changes under the hood?

It is important to note that Deno is not a fork of Node , but a new implementation based on Google V8 and written in Rust. This contrasts sharply with Node, which is written in C++.

Does the use of C++ make Node the faster runtime?

The answer to this question depends on your site traffic; Deno performs better than Node when there is more concurrent connections. This is explained better in this handy article .

Want to give Deno a shot? Head over to the tooling section , which goes over the build and debugging tools to help kick things off!

Tags

Node
Deno
menu_icon
Gabriel Stellini

29th September 2021