- vừa được xem lúc

Blog#168: 🚀Nodejs Best Practices in 2023✅

0 0 13

Người đăng: NGUYỄN ANH TUẤN

Theo Viblo Asia

Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.

Do not use the functions JSON.parse and JSON.stringify

I have been using this for a long time and it is very important to me, but when the amount of data I put in increases, it takes a lot of time and can slow down the process. As the amount of data increases, the time it takes also increases. If my application takes in and processes large JSON objects from users, I should be careful about how much data I take in.

The reason why these functions are taking a long time to complete is because they are trying to process the entire input at once. To make them faster, you can use a stream to break the object or string into smaller chunks. There are some npm modules that offer asynchronous JSON APIs. If you are dealing with small to medium size data, JSON.parse and JSON.stringify will work fine, but if you are dealing with large data sets, you should look into these other options:

Add a logger

Logger libraries can help you keep track of messages, errors, and successful requests that your application receives. Morgan is a library that allows you to store logs about HTTP requests that have been sent. By adding Morgan as middleware, it will write the logs to the console. You can also look into other storage mechanisms to store the logs. Morgan will help you analyze the data and improve your application.

var express = require('express')
var morgan = require('morgan') var app = express() app.use(morgan('combined')) app.get('/', function (req, res) { res.send('hello, world!')
})

I suggest using Winston for keeping track of errors and messages. It is simple to use and can store information in different places like files, databases, and the console. You can also choose the level of detail you want to see, like notice, error, warning, and info.

What I normally do is create a separate logger file and export it.

const { createLogger, format, transports } = require('winston');
const config = require('./config');
const { combine, printf } = format;
const winstonFormat = printf( ({ level, message, timestamp, stack }) => `${timestamp} ${level}: ${stack || message}`
);
const { timestamp } = format;
const logger = createLogger({ level: config.env === 'development' ? 'debug' : 'info', format: combine( timestamp(), winstonFormat, config.env === 'development' ? format.colorize() : format.uncolorize() ), transports: [new transports.Console()],
});
module.exports = logger;

Choose one API to use for one specific task

It can be hard to organize APIs, but this method can help make them more independent, easier to maintain, and divide up tasks. It also makes your application run faster than if you put all the operations in one API.

For example, when creating a form that accepts both text and videos, it is best to have two separate APIs: one to store the text and one to upload the video in real time. This will make the response time faster.

Separate code into npm packages

If you are working on multiple projects and find yourself using the same code over and over again, it is a good idea to put that code into an npm package. This will save you time in the long run, and make it easier to collaborate with others.

Make heavy calculations asynchronous

Node JS is good at dealing with input and output operations, but it is not suitable for complex calculations that take a long time. If you try to add a large number of integers, your application may become stuck and unable to serve requests, because Node JS only uses one thread to handle all requests, and does not create a new thread for each request.

If the Event Loop takes too long to process a task, all current and new requests will not be processed. We need to find a solution quickly!

You can use the setImmediate function to make sure that your blocking function is given priority in the event loop. This means that Node.js will prioritize the initialization, assignment, and registering of asynchronous codes before running the setImmediate function.

// other codes setImmediate(() => { processData(data); });
// other codes

The event loop does not immediately run the processData function. Instead, it registers the set immediate function and allows other code to run first. If you need to do something complicated, the first approach is not the best choice. This is because it only uses the Event Loop, so it won't take advantage of multiple cores. The better solution is to use the Node JS worker_threads module to create a separate thread to do those tasks.

Do not store a lot of information in one variable.

Variables are stored in RAM (Random Access Memory) , which makes it quick to save and access them. If you need to store a lot of data, it is best to put it in a database, because if the data is too large, it can slow down the server and your application.

Avoid

You should avoid doing things that are not the best option, even though it might seem humorous. Make sure to take these three points seriously.

  • Do not use the version of the function that requires you to wait for it to finish before continuing, such as readFileSync. Instead, use the version that allows you to continue with other tasks while it is running, such as the callback or promise version.
  • Don't put a lot of information in the request or response when you are sending data, because it can slow down the response time. If you need to send a lot of data, use a streaming method instead.
  • Don't use a lot of data, like big JSON files, with Node.js require because it will slow down the main thread. Put your data in a database and only get what you need. If you have to store it in a file, use streaming to get parts of the data in an asynchronous way.

And Finally

As always, I hope you enjoyed this article and learned something new. Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you. 😊

Resource

Bình luận

Bài viết tương tự

- vừa được xem lúc

Giới thiệu Typescript - Sự khác nhau giữa Typescript và Javascript

Typescript là gì. TypeScript là một ngôn ngữ giúp cung cấp quy mô lớn hơn so với JavaScript.

0 0 525

- vừa được xem lúc

Bạn đã biết các tips này khi làm việc với chuỗi trong JavaScript chưa ?

Hi xin chào các bạn, tiếp tục chuỗi chủ đề về cái thằng JavaScript này, hôm nay mình sẽ giới thiệu cho các bạn một số thủ thuật hay ho khi làm việc với chuỗi trong JavaScript có thể bạn đã hoặc chưa từng dùng. Cụ thể như nào thì hãy cùng mình tìm hiểu trong bài viết này nhé (go).

0 0 433

- vừa được xem lúc

Một số phương thức với object trong Javascript

Trong Javascript có hỗ trợ các loại dữ liệu cơ bản là giống với hầu hết những ngôn ngữ lập trình khác. Bài viết này mình sẽ giới thiệu về Object và một số phương thức thường dùng với nó.

0 0 153

- vừa được xem lúc

Tìm hiểu về thư viện axios

Giới thiệu. Axios là gì? Axios là một thư viện HTTP Client dựa trên Promise.

0 0 145

- vừa được xem lúc

Imports và Exports trong JavaScript ES6

. Giới thiệu. ES6 cung cấp cho chúng ta import (nhập), export (xuất) các functions, biến từ module này sang module khác và sử dụng nó trong các file khác.

0 0 110

- vừa được xem lúc

Bài toán đọc số thành chữ (phần 2) - Hoàn chỉnh chương trình dưới 100 dòng code

Tiếp tục bài viết còn dang dở ở phần trước Phân tích bài toán đọc số thành chữ (phần 1) - Phân tích đề và những mảnh ghép đầu tiên. Bạn nào chưa đọc thì có thể xem ở link trên trước nhé.

0 0 245