8 ways to improve performance of your node js application

8 ways to improve performance of your node js application

  1. Using worker threads

Node js is best at handling IO, database, and network operations asynchronously but when you do operations like the Fibonacci series that require high CPU computation the main thread can be blocked. And the greater the number it computes it will not even serve requests. By separating time taking and CPU-intensive tasks into worker threads we can easily increase the performance of our app.

2. Serving static content by NGINX

NGINX is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.

This separates serving static files from node js main thread also, Since Nginx is purpose-built for serving web content it is much better than node js in serving static content and supports caching.

3. By indexing repeatedly searched/used columns

Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Hence helps to optimize the performance of a database by minimizing the number of disk accesses required when a query is processed. Index implementation depends on the database you used but, every database indexing aims for quick access avoiding scanning through all rows.

4. By choosing the deployment server region near your users

Different cloud services give you a choice to choose a region. A region is a geographical location the server is located at. Choosing the region near the users helps reduce data latency since data would pass faster.

5. By caching time-intensive query results using an in-memory database

In-memory databases are purpose-built databases that rely primarily on memory for data storage, in contrast to databases that store data on disk or SSDs which increase response times by eliminating the need to access disks. database like Redis is used for caching and can help you access data fast and easily. So when you have to do complicated, time-intensive operations that need to be sent through response, you can cache the result so that it can be sent later when it is requested. You can also cache complex database queries that take too long to fetch specific information.

6. Using process management

Process managers tool like pm2 lets you use multiple CPU cores to handle multiple requests at a time. It also keeps the application up and running and gives logs reporting load balancing, and microservice management.

7. Using stream

Stream is a way to pass a small chunk of data rather than reading big files at once. Whenever you have a big file that you want to send to a user such as videos or when a user uploads a file and you want to process that file, stream helps you send a small part of the data one by one. This improves performance because the request/response carries a small amount of data resulting in fast delivery. Netflix and youtube are the best examples that use stream. In node js there is a stream module that provides an API for implementing the stream interface

8. By scheduling tasks to run in the background

When you need to do a task such as sending an email after a user paid, don’t do it right away while the user is waiting for results, register it to a queue and schedule it to run in the background. Queues are used to store instructions or messages that will be executed in First In First out (FIFO) manner. You can listen to events when queues are stored and process them and mark them as done. This can give you better management of the messages and helps you process instructions without having confused about which ones to process first, which are done, and which are not. Npm packages like bull can give helps you to implement queue and many cloud services also have queue services.

— — — — — — — — — — — — — — — — — —

Follow me on linked in and get the full advanced node js course I will release soon.

To view or add a comment, sign in

More articles by Kal T.

Others also viewed

Explore content categories