Delicious value: 🌟🌟🌟🌟🌟

Taste: Curry fat beef pot

Eating guide

This is Wolf’s Reaction to his live broadcast of “Learning Means North: Node.js 2022 Full Analysis” in Nuggets on February 16. There are a lot of dry things in the live broadcast of Wolf, and this article will try to keep the original content while adding some related materials and links for your learning.

Node.js 2021

  • Node 2021 developer report link

Node 2021 developer report parsing

Node.js isn’t so popular now? It’s not.

From the node.js source update content, Node.js is healthy and stable development, from usable to usable in the transition, in the ease of use has been greatly improved. The performance card is played, and the “user experience” card is played.

Core outputs async_hooks, profiling, tracing, Dump debug, report, etc. It allows developers to improve the development experience of Node.js applications with a lower barrier to entry.

1. The development framework has changed greatly, with fewer wheels, more TS and more enterprise-level frameworks

The popularity of Egg.js in China is high, while Midway and Nest are growing fast, partly due to TypeScript’s popularity.

2. The version changes greatly and the upgrade is active

Node12 was used last year, and in 2021, Node14 will account for nearly half of the total.

(if you want to optimize performance, the best way to do this is to upgrade the Node version 🙂

3 spit more bad, means that more people use, tend to mature

“There are only two languages in the world, the one that nobody uses and the one that gets complained about.”

4. Out of the loop: The age distribution is larger than last year, and the types of work are more diversified

The whole Node community is no longer just about front-end engineers. Back-end, full-stack engineers, architects, and even operation and maintenance, technical directors all have certain contacts, and students and interns also have certain usage.

5. Usage confusion: performance optimization, memory leaks, and NPM dependencies

6. Future: The more experienced I am, the more I will focus on performance and Serverless

  • The more experienced you are, the more interested you are in WebAssembly (WASI) and N-API.
  • The younger the students are, the more concerned they are with Serverless.

The trend behind the good stuff

1.clipanion

  • Clipanion warehouse link
class FooCommand extends Command {
    static paths = [[`foo`]].async execute() {
        this.context.stdout.write(`Foo\n`); }}class BarCommand extends Command {
    static paths = [[`bar`]].async execute() {
        this.context.stdout.write(`Bar\n`); }}Copy the code

The TS + Object-oriented + template pattern is very elegant.

Clipanion is a type-safe CLI library with no runtime dependencies. It is used in the Yarn V2 version of berry source code. It is also used in Node.js, including Corepack(package manager for NPM, YARN, PNPM, CNPM, etc.).

It can be seen that the community attaches great importance to it. Although its Star number is not high, it is still worth learning.

Compare the common CLI libraries Commander. Js and CAC, which are written as functions by defining a function and passing in arguments using option.

Depending on which way you like to write it, they’re all good choices.

// Commander.js
const { program } = require('commander');

program
  .option('--first')
  .option('-s, --separator <char>');

program.parse();

const options = program.opts();
const limit = options.first ? 1 : undefined;
console.log(program.args[0].split(options.separator, limit));
Copy the code
// examples/basic-usage.js
const cli = require('cac')()

cli.option('--type <type>'.'Choose a project type', {
  default: 'node',})const parsed = cli.parse()

console.log(JSON.stringify(parsed, null.2))
Copy the code

2. Recommendation of 10 TS related projects

  • CLI module: Clipanion
  • Test module: UVU
  • Web module (Express alternative) : TinyHTTP
  • Database module: TypeORm
  • Web applications: Midway, DarUK, Loopback, Nest, SSR
  • React Bucket: Umi

2.1 the find – my – way

  • find-my-way
const http = require('http')
const router = require('find-my-way')()

router.on('GET'.'/'.(req, res, params) = > {
  res.end('{"message":"hello world"}')})const server = http.createServer((req, res) = > {
  router.lookup(req, res)
})

server.listen(3000.err= > {
  if (err) throw err
  console.log('Server listening on: http://localhost:3000')})Copy the code

Inspired by the echo framework routing implementation in Go language, the specific implementation is based on cardinality tree. Part of the code is extracted from Trekjs.

performance

The find-My-way benchmark in All Together is 525,798 ops/ SEC and the KOA-Router benchmark is 161,220 ops/ SEC. The difference in the number of requests per second that can be processed is still very significant, and the cardinality tree approach is much faster than the regular approach.

======================= find-my-way benchmark ======================= short static: Ops/SEC mixed static dynamic route: 1,637,929 ops/ SEC mixed static dynamic route: Ops/SEC long static: 5,403,719 OPS/SEC wildcard: 3,037,119 ops/ SEC all together: 525798 ops/SEC = = = = = = = = = = = = = = = = = = = = = = koa - the router benchmark = = = = = = = = = = = = = = = = = = = = = = short static: Ops/SEC Mixed static with Same radix: 1,029,369 OPS/SEC Dynamic route: 1,015,635 OPS/SEC mixed static dynamic Route: Ops/SEC long static: 1,027,857 OPS/SEC Wildcard: 1,033,432 ops/ SEC all together: 161,220 ops/ SECCopy the code

The chart below is taken from Fasify’s benchmark

Both Fastify and Restify are find-my-way based routes. Earlier versions of Restify were express based routes, and Fastify was able to handle more requests per second than native. Optimization from the perspective of data structure is an important point to improve performance in the future.

  • Radix Tree principle
  • path-to-regexp

2.2 Four Suggestions for Performance Optimization

  • fast-json-stringify

2.3 Test framework evolution

idea

Build wheels: Put junit apis into TS.

Realize the path

ts-import vs typescript-require

The performance of these two is still a little bit poor, so we will use Rust or Go compiler to rewrite.

xv

In the course of building the wheel, an interesting library xv was discovered. His other works include JSON-Server and LowDB.

The source code is only 40-odd lines long, without defining any test syntax.

  • github.com/typicode/xv

uvu

Uvu source value learning.

Common Node testing frameworks:

  • ava
  • jest
  • mocha
  • tape
  • uvu

vitest

Will replace Jest and become more and more popular.

  • vitest

2.4 easy to monitor

  • Easy-monitor is the best performance monitoring solution in recent years.

Monitoring spot

Some pain points are a little bit more demanding for developers, but Easy-Monitor has done it for us.

  1. Performance tuning is timeless
  2. Based on Addon, there is no version dependency. Various memory leak analysis tools and Coredump analysis tools, absolutely have your knowledge blind spot
  3. It is very helpful to read the node source code and understand the server in depth
  4. It is also helpful to understand libuv and c++
  5. Source code based on Egg and Vue, are very mature technology
  6. The author must be reliable

Comprehensive procedure for locating application faults

2.5 rushstack

Turborepo is a competitor to Lerna, and currently doesn’t compete with the RushStack, which is designed for very large projects. Microsoft also uses the rushstack for its entire range of products. Rushstack is a good practice for future mega-project challenges.

conclusion

Eight Web frameworks

  • Jamstack, the next generation of Web building technology stack?

Evolution of architecture

This party is based on a Node subway sharing, which is linked below.

  • The practice of JavaScript full stack efficient research and development in Language Sparrow

The front-end 3.0

Rethink the relationship between the front and back ends

BFF vs FFB

There is so much glue in the front-end code that essentially it should be a very clean process from the database to the API fields and then to the front-end rendering. However, due to business development or various situations, the fields in the database cannot be taken out and used directly, and the communication cost of the front and back ends leads to the addition of additional logic in the middle. As a result, the complexity of the front end is increasing.

  • For more information, please refer to uncle Wolf’s answer in the community

❤️ Love triple punch

1. If you think the food and drinks in the canteen are ok with you, please give me a thumbs-up. Your thumbs-up is my biggest motivation.

2. Pay attention to the front canteen of the public account and eat every meal!

3. Like, comment, forward === urge more!