deno.js_Deno vs. Node.js —主要区别

deno.js

Should I learn Deno? What's the main benefit of using Deno over Node?

我应该学习Deno吗? 在节点上使用Deno的主要好处是什么?

After reading this story you'll know key principles of how Deno works, who is behind it, and why there are so many discussions about it. I'll compare it with Node.js as it is the most popular runtime for JavaScript so far.

阅读此故事后,您将了解有关Deno工作原理,幕后人物以及为何对此进行如此众多讨论的关键原则。 我将它与Node.js进行比较,因为它是迄今为止JavaScript最受欢迎的运行时。

Node has the same creator as Deno, Ryan Dahl. The right question is then, “why has he created another library if Node is already settled in the market and favored by many companies?”

Node与Deno,Ryan Dahl具有相同的创建者。 正确的问题是:“如果Node已经进入市场并受到许多公司的青睐,他为什么还要创建另一个库?”

幕后的支持 (The Support Behind the Scene)

Official docs say:

官方文档说:

“Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 engine and is built in Rust.”

“ Deno是使用V8引擎并内置于RustJavaScript和TypeScript的简单,现代且安全的运行时。”

That is also the main reason why Deno could be very successful. There are big companies behind it:

这也是Deno能够取得成功的主要原因。 背后有大公司:

  • Rust created by Mozilla

    由Mozilla创建的Rust
  • TypeScript from Microsoft

    Microsoft的TypeScript
  • V8 engine from Google

    Google的V8引擎

On the other hand, Node is built in C and C++, which tend to be less secure compared to Rust. It doesn't have TypeScript compiler built in, so in order to use it, you need to install it with npm.

另一方面,Node是用C和C ++构建的,与Rust相比,它们的安全性较差。 它没有内置的TypeScript编译器,因此要使用它,您需要使用npm进行安装。

What they have common is the V8 engine from Google under the hood — the same one that drives the Chrome browser.

它们的共同点是引擎盖下的Google V8引擎-驱动Chrome浏览器的引擎。

ES模块与CommonJS (ES Modules vs. CommonJS)

In 2009, when Node was created, JavaScript didn't have its own module system. Hence, by default, CommonJS syntax is used in Node:

在2009年创建Node时,JavaScript没有自己的模块系统。 因此,默认情况下,Node中使用CommonJS语法:

const bcrypt = require('bcrypt');

The problem is that this isn't really JavaScript. It's a non-standard way of including different pieces of code from third-party libraries.

问题在于,这实际上不是JavaScript 这是包含第三方库中不同代码段的非标准方法。

Fortunately, in 2015 ES6, introduced its own module system for JavaScript, so now you can do this:

幸运的是,2015年ES6引入了自己JavaScript模块系统,因此现在您可以执行以下操作:

import bcrypt from 'bcrypt'

Compared to Deno, ES modules are used in Deno out of the box. That means you can actually import URLs without installing it through npm:

与Deno相比,Deno中使用了ES模块。 这意味着您实际上可以导入URL,而无需通过npm安装它:

import 'https://deno.land/std/examples/chat/server.ts'

With that ability, you can import basically any piece of program and use it in your code. However, if you do it just like that, you will get a permission error.

有了这种功能,您基本上可以导入任何程序并将其用于代码中。 但是,如果您只是这样做,则会出现权限错误。

The reason is that Deno is also keeping security in mind.

原因是Deno还牢记安全性。

安全第一 (Security First)

Although Node is becoming more and more popular, many companies are tending to bypass it. One of the most important reasons is that it has flaws in security.

尽管Node越来越受欢迎,但是许多公司都倾向于绕过它。 最重要的原因之一是它存在安全漏洞。

The issue is that Node is using npm in order to install external modules for the app. And because npm is opened to the community and everyone can create one, one can potentially inject malicious things.

问题在于,Node正在使用npm来为应用安装外部模块。 而且由于npm向社区开放,每个人都可以创建一个,因此一个人可能会注入恶意的东西。

And Node will trust this module without any complaints.

Node将信任此模块,而不会产生任何投诉。

On the other hand, Deno is using the principle of least privilege and the listing of allowable entities. You can grant permissions to each module individually. That itself solves many potential security issues when developing the server.

另一方面,Deno使用最小特权原则允许实体列表 。 您可以分别为每个模块授予权限。 这本身解决了开发服务器时的许多潜在安全问题。

内建工具 (Built-In Tools)

To start to develop an app with Node, you have to decide what tools will be used in the first place.

要开始使用Node开发应用程序,必须首先确定将使用哪些工具。

Are you going to use TypeScript? Then install ts-node. What about testing? Let's use Jest. What about formatting? Bundling? etc.

您要使用TypeScript吗? 然后安装ts-node 。 那测试呢? 让我们使用Jest 。 格式化呢? 捆绑? 等等

With Deno, you don't have to solve these problems because it comes with these tools preinstalled. And since it's standardized for Deno, you can be sure that these tools have full support. Really cool.

使用Deno,您无需解决这些问题,因为它已预装了这些工具。 而且,由于它是Deno的标准配置,因此您可以确定这些工具具有完整的支持。 真的很酷。

浏览器兼容的API (Browser-Compatible API)

I consider this as one of the biggest differences between Node and Deno.

我认为这是Node和Deno之间最大的差异之一。

As you already might know, browsers have window as a global object, which has many properties and methods such as addEventListener, setTimeout, setInterval, etc.

您可能已经知道,浏览器将window作为全局对象,它具有许多属性和方法,例如addEventListenersetTimeoutsetInterval等。

Browsers also use fetch to send a request to the given URL and return Promise.

浏览器还使用fetch将请求发送到给定的URL并返回Promise。

In Deno, you have all these things built-in. This is perfect, especially for front-end developers, who are using these methods daily.

在Deno中,您具有所有内置的东西。 这是完美的,特别是对于每天使用这些方法的前端开发人员而言。

那Node呢? (What about Node?)

In Node, fetch doesn't exist. If you want to use it, you need to install something like node-fetch or node-fetch-npm. But which one is the right one? Will this work in my project?

在Node中, fetch不存在。 如果要使用它,则需要安装诸如node-fetchnode-fetch-npm之类的东西但是哪一个是正确的呢? 这会在我的项目中起作用吗?

Node also use global as the main object, which is not part of the browser. So it's not really JavaScript.

节点还使用global作为主要对象,而不是浏览器的一部分。 因此,它并不是真正JavaScript。

单一可执行文件 (Single Executable)

When using Deno, you always have a single executable file, e.g. deno.exe, which you can double-click and run Deno runtime. So basically you can write programs using JavaScript and then share with other computers with ease.

使用Deno时,始终只有一个可执行文件,例如deno.exe ,您可以双击并运行Deno运行时。 因此,基本上,您可以使用JavaScript编写程序,然后轻松与其他计算机共享。

In Node, there's no such option as a single executable, because the app needs to run with all necessary packages under the node-modules folder.

在Node中,没有像单个可执行文件这样的选项,因为该应用程序需要在node-modules文件夹下的所有必需软件包下运行。

异步返回承诺 (Async Returns Promises)

Asynchronous code in Deno always returns Promise. In Node, that's not always the case.

Deno中的异步代码始终返回Promise。 在Node中,情况并非总是如此。

Node was created before we had Promise-based syntax. Although in many cases asynchronous code returns Promise, sometimes you have to do workarounds with constructors like util.promisify.

在使用基于Promise的语法之前已创建Node。 尽管在许多情况下,异步代码返回Promise,但有时您必须使用util.promisify构造方法来解决。

In fact, anytime a new feature is introduced in JavaScript, Node needs to do some workarounds in order to have them compatible. In contrast, Deno supports the newest JavaScript features out of the box.

实际上,每当JavaScript中引入新功能时,Node都需要做一些变通办法以使其兼容。 相反,Deno开箱即用地支持最新JavaScript功能。

Its back end is written in Rust, which handles asynchronous code with something called futures. Under the hood, it translates asynchronous code into Promises.

它的后端是用Rust编写的,它使用称为future的东西处理异步代码。 在后台,它将异步代码转换为Promises。

This means that in Deno you can use async await without any issues.

这意味着在Deno中,您可以使用async await而不会出现任何问题。

总结事情 (To Sum Things Up)

So, should I immediately start using Deno?

那么,我应该立即开始使用Deno吗?

I would say, “Not really.”

我会说:“不是真的。”

I mean, sure, it looks very promising when looking at all the features and how it's built. It basically solves a lot of issues that Node has in its core.

我的意思是,当然,在查看所有功能及其构建方式时,它看起来非常有前途。 它基本上解决了Node核心存在的许多问题。

Deno is supported by big companies, it uses the newest JavaScript, and its security is on a high level. But every technology has its flaws, of course.

Deno得到大公司的支持,它使用最新JavaScript,并且其安全性很高。 但是,当然每种技术都有其缺陷。

And so far, the biggest flaw of Deno is that it's only in the early stage. The community is just starting to grow. So only time will show us whether it will catch up with languages such as Node, Java, or PHP — or whether it's just initial hype.

到目前为止,Deno的最大缺陷是它还处于早期阶段。 社区刚刚开始发展。 因此,只有时间会告诉我们它是否会赶上Node,Java或PHP等语言-还是仅仅是一时的炒作。

If you enjoyed reading, you might be interested in some of my other stories:

如果您喜欢阅读,那么您可能会对我的其他一些故事感兴趣:

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/deno-vs-node-js-key-differences-8b2f4f63e2c7

deno.js

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值