Why Deno will replace Node.js

background:

The author has been engaged in AI and artificial intelligence related work. Focus on how to use big data and artificial intelligence in e-commerce, finance, and government related services. Some of them have participated in more than a dozen large-scale projects in Guangzhou, Hengqin, Hong Kong, Macau and other large-scale projects, and have considerable experience.

Recently, I have also become a developer who supports the Chinese open source community, and I hope you will give more support. TrackSoul is an open source project for event tracking, a cross-platform solution. It supports small programs on websites, Web APPs, IOS, Android and even WeChat. The only stable way to improve user experience is to rely on user behavior data. Whether it's deciding to optimize user interfaces, marketing, sales, or predictive analytics. Collecting data is not difficult, and maximizing the efficiency of data usage is our focus, and we hope to provide a holistic solution from collection to analysis. We hope to build a long-term maintainable ecosystem for this project to expand the freedom of user behavior. This project not only provides a Javascript toolkit for collecting data, but also a complete user behavior analysis platform!

https://gitee.com/CharlesArea/TrackSoul

Overview (preface):

Node.js (hereafter referred to as Node) has always been popular with the JavaScript programmers community, and even having all the languages ​​would not be able to scale the huge community of NPM suite management tools. Node was created by Ryan Dahl, and before Node came out, you and I probably didn't think about using javascript to develop both the server and the client side at the same time, which meant developers only needed to use one language for each layer. That said, it's trivial for a web developer to switch from other front-end technologies to Node.

However, in 2018, the father of Node development publicly admitted in JSconf ten mistakes made when designing and updating Node. Among them, security, memory leaks, and callback hell problems caused many developers to dare not work in some large projects. Use Node to develop. To this day, there are many problems arising from nodes in major communities every day, including "async-await inside Node is not really synchronized", "there are a lot of libraries with uneven quality and disrepair in the npm warehouse, which lead to security risks. The problem", "After using npm install, I found that the application downloaded a node module of several hundred mb", etc. Of course, the most effective way is to optimize from the alternative design of Node, but as of today, there is no way to do any major expansion and optimization, because there are already too many Javascript projects based on Node, including a fairly large NPM suite library. So why don't we support and try to choose a better solution?

Deno is also a Javascript execution environment led by Ryan Dahl, and Deno 1.0 was finally officially launched on May 13, 2020, and I myself have been waiting for more than a year. The name Deno is a recombination of letters from Node, which means "clean up Node.js" (de = destroy clean up, no = Node). Although it is in an early stage of development, if you are a Javascript programmer now or are interested in a career in web development in the future, today is the best time to start realizing that the future has the most potential to replace Node. Deno. It can effectively solve various problems of Node's "inherent flaws" from the design of overlapping code. I want to make it clear first, I don't think Deno can completely replace the existing part of Node in the next year or even three years, because Node's ecological environment has matured and Deno is still in the development stage of 1.0, but in the future I believe it will be better Disadvantages are inevitable in software development. Now I will lead you to explore Node's problems and Deno's solutions.

Problems with Node Modules:

1. Modules download black hole


You should have encountered the same situation as the author. After npm install, after waiting for a long time, I found that npm downloaded hundreds of mb of node modules. This is because of the related mechanism of "Vendored-by-default" in Node. When you download a node_module, it will automatically download all the modules of dependencies (dependencies) together. The author has also tried it before. Not long after the project started, the number of code pushed by developers is only a few thousand lines, but node_modules The amount of code inside has reached hundreds of thousands of lines. This design makes the way to deal with node_module super complicated. Ryan Dahl also publicly stated "It's my fault and I'm very sorry. Unfortunately it's impossible to undo now." Troubled, even some foreign netizens drew the following picture to show that Node modules are deeper than black holes, for example.

On the other hand, Deno has completely abandoned the previous approach introduced by CommonJS of Node modules and opted to use ES modules instead. I believe that readers who have done front-end development should already be familiar with it. When using ES modules, you no longer need to install any modules through npm. Users only need to import from the url in the web server. When you want to use something like http/ When using express framework, you can directly use import url like below

import { serve } from "https://deno.land/[email protected]/http/server.ts";

2. Introduce module


You may be familiar with it. When we introduce a module into the project after npm install, we don't actually need to enter the extension of .js, just const http = require('http') . In fact, the tag in the <script> in the browser cannot omit the ".js" extension, which makes Node perform unnecessary file system queries several times before it can guess what file we want to import. A situation that causes the operation to slow down occurs.

3. Standard library

Most of Node's modules are based on third-party packages, even some very commonly used packages, because in NPM, there are quite a few packages with the same function, and most readers should have encountered similar problems, because Node's official API /package is not very complete. Deno is very characteristic in the standard library, and it also provides official versions for common functions to ensure usability and continuous stability. The following also lists some comparisons with the npm third-party library

This shows that Deno is not just an operating environment, it will be a complete basic ecology, alleviating various problems of uneven quality in the Npm ecology, and greatly improving the stability of the underlying library. But of course, the Deno ecosystem also has third-party libraries, and in essence, there are no functional barriers between the third-party library and the official library, because the implementation codes are similar.

safety

The security of the software/website platform is one of the most important factors to determine whether the project can be launched and whether the user's information is safe or not. Let's see what happens if we use Node to create an http server.

First we need to create an index.js, then use npm install to install the required Node_modules (such as http/Express), the code is as follows

const http = require('http');
const server = http.createServer((req,res) =>{
    res.end('OpenSourceChina by Charles Lo');
});

const port = 3000;
server.listen(port);
console.log("HTTP web built with the port " + port);

I believe you may already be familiar with the related usage methods, and even many people who use Node are accustomed to using the Express application framework. The code is as follows

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('OpenSourceChina by Charles Lo'))
app.listen(port, () => console.log(`HTTP web built at http://localhost:${port}`))

In fact, both are very similar, and we've always loved how simple, intuitive it is to operate. So let's see what happens if we use Deno to develop?

We can create an index.ts, by the way, here you can already see that it is a little different from Node, because in addition to supporting Javascript, deno can also support Typescript at the same time.

We can try to create a http server in index.ts

import { serve } from "https://deno.land/[email protected]/http/server.ts";

  const s = serve({ port: 3000 });

  console.log("HTTP web built at http://localhost:3000/");

  for await (const req of s) {

    req.respond({ body: "IBM developerworks by Charles Lo\n" });

 }

Now we can try to run the relevant code. In fact, we can also see some styles of Node when deno is executed.

deno run index.ts 

You can see that when Deno is compiled, it will automatically download the files related to the Import URL, and then store them in the local cache (buffer memory), so there is no need to download the related files again after execution. In a nutshell, this is a complete replacement for all of npm's ills, as if npm would automatically install a huge modules dependencies folder.

error: Uncaught PermissionDenied: network access to "0.0.0.0:3000", run again with the --allow-net flag

You can also see that Deno has not been executed normally, and a "security" related error has occurred. Because Deno is a "safe" operating environment, it is not like Node. When you npm install the package, you don't know whether the scripts in the package are safe or not. All scripts can directly create a web server or read and write privately. Any files and files in your local, the script can perform any action on your computer/server. However, Deno does not allow scripts to privately create a web server or read your files without permission.

Now we can try using --allow-net to allow Deno to create a web server.

We finally successfully created and entered the Web page. Of course, --allow-net only allows deno to create a website service, and all reading and writing of files in the system remains prohibited. The following are the parameters related to security control in deno

* --allow-read:打开读权限,可以指定可读的目录,比如--allow-read=/desktop/ibm

* --allow-write:打开写权限

* --allow-net=developer.ibm.com:允许网络通信,可以指定可请求的域,比如--allow-net=developer.ibm.com

* --allow-env:允许读取环境变量

You should also be able to feel the difference in security between Deno and Node.

Build system:

You can see the above schedule. Chrome V8 was released in 2008. At that time, the V8 team developed a build system called GYP (Generate Your Projects) according to the characteristics of their own projects. When Node came out in 2009, Node The JavaScript Engine V8 used is also built using GYP as a matter of course, but then V8 switched to GN (Generate Ninja), which means that Node is the only user left. The data shows that the build system using GN is nearly 20 times faster than GYP written in Python, which is a world of difference for users. Ryan has also publicly stated that "The continued usage of GYP is the probably largest failure of Node core."

Async issues:

In fact, as early as June 2009, Node had begun to introduce JavaScript Promises, but it was removed in February 2010 (I don't quite understand Ryan's original idea). Therefore, in order to achieve similar functions, users in different periods have developed unofficial versions of the Promise function by themselves. This leads to the fact that Node is still full of different async API designs with async/await and promises today, which greatly increases the maintenance cost. Now the Node.js API is still based on the callback function, and there is no way to use promises and async/await gracefully.

When you just saw the use of deno to create a web server, you may have noticed the following code

for await (const req of s) {

    req.respond({ body: "IBM developerworks by Charles Lo\n" });

  }

In fact, when Deno was designed, it has emphasized that top-level await must be supported. For example, when we read files in Node, we need -

const fs = require("fs");

fs.readFile(“./ibm.txt”, (err, data) => {

  if (err) throw err;

  console.log(data);

});

We still need to use callbacks to handle asynchronous operations. But in deno directly choose to use Promise -

const data = await Deno.readFile(“./ibm.txt”);
console.log(data);

Although the strong Node ecosystem has been improving various problems derived from Node, for example, using promisify is as follows

const { promisify } = require("es6-promisify");
const fs = require("fs");

async function main() {
    const readFile = promisify(fs.readFile);
    const data = await readFile(“./oschina.txt);
    console.log(data);
}

main();

But in the end, there is still no top-level-await, and only one layer of operations can be included

concluding remarks

In fact, Deno still has a lot to explore, and the Deno v1.0 development team also promises "As of Deno 1.0.0, the Deno namespace APIs are stable. That means we will strive to make code working under 1.0.0 continue to work. in future versions.”, which shows that the API of Deno 1.0.0 is stable. With the iterative updates of Deno in the future, most of the APIs that can be used today will also be available in future Deno versions. On the whole, Deno can effectively solve all the problems you have encountered when writing Node. It is very worth exploring and researching. I also hope that in the near future, I can continue to share more different fields with you and look forward to it together. The next Deno release.

Reference resources

https://deno.land/ Deno Homepage

https://github.com/denoland/deno Deno Github open source project address

https://gitee.com/CharlesArea/TrackSoul TrackSoul is an open source project, cross-platform solution for event tracking. It supports small programs on websites, Web APPs, IOS, Android and even WeChat. The only stable way to improve user experience is to rely on user behavior data. Whether it's deciding to optimize user interfaces, marketing, sales, or predictive analytics. Collecting data is not difficult, and maximizing the efficiency of data usage is our focus, and we hope to provide a holistic solution from collection to analysis. We hope to build a long-term maintainable ecosystem for this project to expand the freedom of user behavior. This project not only provides a Javascript toolkit for collecting data, but also a complete user behavior analysis platform!

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324139479&siteId=291194637