Language switcher

Light

Dark

There are several ways to optimize a Node.js application, including:

  1. Use the latest version of Node.js: Newer versions of Node.js generally have performance improvements and bug fixes.
  2. Use a profiler: A profiler can help you identify areas of your code that are causing performance bottlenecks, such as slow function calls or memory leaks.
  3. Minimize the use of synchronous functions: Synchronous functions block the event loop, preventing other code from executing. Use asynchronous functions instead, such as those provided by the async and await keywords.
  4. Use a content delivery network (CDN): A CDN can help distribute your application’s static assets, reducing the load on your server.
  5. Use a reverse proxy server: A reverse proxy server, such as NGINX or HAProxy, can help handle load balancing and SSL termination, freeing up your Node.js server to handle other tasks.
  6. Use caching: Caching can help speed up the performance of your application by storing the results of expensive operations, such as database queries, in memory.
  7. Optimize the database queries
  8. Use a process manager like pm2 for better process management.

1. Use the latest version of Node.js: Newer versions of Node.js generally have performance improvements and bug fixes.

Using the latest version of Node.js can help improve performance and security. The Node.js development team is constantly working to improve the performance of the platform. Updating to the latest version can give you access to new features and performance improvements. However, it’s important to make sure that your application is compatible with the new version before updating.

Also, make sure to keep track of the release notes, which indicate the new features, bug fixes and performance improvements that came along with the version. This can give you an idea of what areas of your application might be impacted by the update.

2. Use a profiler: A profiler can help you identify areas of your code that are causing performance bottlenecks, such as slow function calls or memory leaks.

Using a profiler is an effective way to identify performance bottlenecks in your Node.js application. Profilers allow you to track the performance of your application in real-time, providing detailed information about CPU and memory usage, function calls, and other metrics. This information can help you identify areas of your code that are causing slowdowns or memory leaks, and make the necessary changes to improve performance.

There are several profilers available for Node.js, such as the built-in profiler in Node.js, the v8-profiler, and the node-inspector. Some popular third-party profilers are :

  • node-clinic
  • nodetime
  • strong-trace
  • node-memwatch

It’s also worth noting that you can use performance tooling in the browser as well to monitor the performance of client-side JavaScript.

It’s a good idea to profile your application in different scenarios, such as when it’s under load, to get a better understanding of how it behaves under different conditions.

3. Minimize the use of synchronous functions: Synchronous functions block the event loop, preventing other code from executing. Use asynchronous functions instead, such as those provided by the async and await keywords.

In Node.js, the event loop is responsible for managing the execution of JavaScript code. Synchronous functions block the event loop, preventing other code from executing, which can lead to poor performance and slow response times. Asynchronous functions, on the other hand, allow other code to execute while they are running, which can help improve the overall performance of your application.

The async/await keywords are a way to write asynchronous code that looks and behaves like synchronous code. They allow you to write asynchronous code using a familiar syntax, making it easier to understand and maintain.

For example, instead of using the callback pattern:

You can use async/await like this:

It’s important to note that not all functions are asynchronous and not all synchronous functions are a bottleneck in the application. It’s good to evaluate the use-case and then decide whether to make the function asynchronous or not.

It’s also worth noting that not all parts of your application will benefit from using asynchronous functions, for example, IO-bound operations like reading/writing from a file, or connecting to a database, are good candidates for async functions, while CPU-bound operations like heavy computation are not.

4. Use a content delivery network (CDN): A CDN can help distribute your application’s static assets, reducing the load on your server.

A Content Delivery Network (CDN) is a network of servers located in multiple geographic locations, designed to deliver content, such as images, videos, and other static assets, to users with low latency and high availability.

When a user requests a static asset from your application, the request is routed to the nearest CDN server, which then delivers the asset to the user. This can help reduce the load on your server, as well as improve the performance of your application for users located far away from your server’s location.

CDN also has the added benefits of :

  • Improved security through DDoS protection and SSL offloading
  • Caching of static assets, which can reduce the number of requests to your origin server.
  • Reducing the load on origin server and improve the scalability of your application.

There are several CDN providers available, such as Cloudflare, Amazon CloudFront, Akamai, and many more. You can choose the provider that best fits your needs and budget.

It’s worth noting that using a CDN is not always necessary for all types of applications, for example, if your application serves a small number of users and the majority of them are in the same location as the origin server, the benefits of a CDN may be negligible.

5. Use a reverse proxy server: A reverse proxy server, such as NGINX or HAProxy, can help handle load balancing and SSL termination, freeing up your Node.js server to handle other tasks.

A reverse proxy server is a server that sits in front of one or more web servers and forwards incoming requests to the appropriate server. Reverse proxy servers can be used to handle a variety of tasks, such as load balancing, SSL termination, and caching.

Load balancing is the process of distributing incoming traffic across multiple servers to ensure that no single server is overwhelmed. Reverse proxy servers can distribute incoming requests to multiple servers based on various algorithms, such as round-robin or least connections.

SSL termination refers to the process of handling SSL encryption and decryption before the request reaches the web servers. By handling SSL termination at the reverse proxy server, the web servers can handle unencrypted requests, reducing the load on the web servers and improving performance.

Caching refers to the process of storing frequently requested data in memory, so that it can be served quickly without having to generate the response from scratch. Reverse proxy servers can cache responses from web servers, reducing the load on web servers and improving the performance of the application.

NGINX and HAProxy are popular open-source reverse proxy servers that can be used to handle these tasks. Both are widely used, have active communities and are highly configurable. It’s worth noting that there are other reverse proxy servers available as well, like Apache, Traefik, and so on. It’s good to evaluate the requirements of the application and choose the one that fits the best.

6. Use caching: Caching can help speed up the performance of your application by storing the results of expensive operations, such as database queries, in memory.

Caching is a technique that stores the results of expensive operations, such as database queries, in memory, so that they can be served quickly without having to generate the response from scratch. This can greatly improve the performance of your application, especially when dealing with frequently requested data.

There are different types of caching that can be used, such as:

  • In-memory caching: This type of caching stores data in the memory of the application server. It is fast but volatile, meaning that the data will be lost if the server is restarted.
  • Disk-based caching: This type of caching stores data on the disk of the application server. It is slower than in-memory caching but more persistent.
  • Distributed caching: This type of caching stores data across multiple servers in a distributed manner. It is useful for applications that are scaled horizontally across multiple servers.

There are several caching solutions available for Node.js, such as memcached and redis. These are both in-memory caching solutions that can be used to store frequently requested data in memory for quick access.

It’s also worth noting that caching should be used with care, as caching stale data can lead to bugs and inconsistent data. It’s important to have a strategy in place for invalidating and updating the cache when the data changes.

Also, It’s good to measure the performance of the application with and without caching to have a clear idea of the benefits it brings to the table.

6. Optimize the database queries

Optimizing database queries is a key step in optimizing a Node.js application. Poorly written or inefficient queries can cause slow performance and high resource usage, leading to poor user experience.

There are several ways to optimize database queries, such as:

  1. Use indexes: Indexes can improve the performance of queries by allowing the database to quickly locate the required data. It’s important to use the right indexes for the queries, as too many indexes can slow down the performance.
  2. Use prepared statements: Prepared statements can improve the performance of queries by allowing the database to reuse the parsed query plan, reducing the overhead of parsing the query each time it’s executed.
  3. Use pagination: Retrieving all the data at once can be slow and consume a lot of memory. Pagination allows you to retrieve a specific number of rows at a time, reducing the load on the database and improving the performance of the application.
  4. Use caching: Caching the results of frequently executed queries can help speed up the performance of the application by avoiding the need to query the database each time.
  5. Use explain plan: Many databases have an explain plan feature that allows you to see how a query will be executed by the database, which can help you identify performance bottlenecks and make the necessary adjustments.
  6. Use ORM (Object-relational mapping) libraries like sequelizemongoose etc. to handle the database queries. These libraries provide a higher level of abstraction, and also provide a lot of performance optimization options.

It’s worth noting that different databases have different performance characteristics and might require different optimization techniques. It’s good to have a good understanding of the database and its performance characteristics in order to make the best use of it.

7. Use a process manager like pm2 for better process management.

Using a process manager like pm2 can help improve the process management of your Node.js application.

pm2 is a popular process manager for Node.js applications. It allows you to run multiple Node.js applications on a single server and handle tasks such as starting, stopping, and restarting applications, as well as monitoring the health of the applications.

Some of the features offered by pm2 are:

  • Automatic restarts: pm2 will automatically restart the application if it crashes or is stopped.
  • Load balancing: pm2 can automatically balance the load across multiple instances of the same application.
  • Monitoring: pm2 can monitor the health of the applications, including CPU and memory usage, and provide detailed information about the status of the applications.
  • Log management: pm2 can handle log management for your application, including log rotation, and aggregating logs from multiple instances of the same application.

By using a process manager like pm2, you can improve the reliability and performance of your Node.js applications. It can also help you to keep your node.js application running even if the server restarts.

It’s worth noting that pm2 is not the only process manager available for Node.js, for example, forevernodemon are other examples. It’s good to evaluate the features and choose the one that fits the requirements of the application.


These are just a few examples, but there are many other ways to optimize a Node.js application.

The idea of an office map was brought up by a real problem our growing company encountered. 

Finding one of 350 employees in an 8-floor office building like Onix’s may be challenging. “Alex B.? Well, on the 7th floor, in the left wing, you turn to the left, then to the right, and then… His was the third desk, I guess. You can ask someone on the spot.” – instructions like this should sound familiar to any big company employee. If you’re lucky, the first colleague you meet on the spot will tell you. Or not. And if you haven’t met Alex B. before, locating him in an open space isn’t an option.

Why we created the office map

When the map was envisioned, the number of Onix’s employees was significantly lower. The company’s office floors were few but not adjacent: the ground floor, the 5th, and the 7th. Finding a colleague would often equate to a mini-workout for those who hate elevators.

The creation of the map implied several capabilities for the employees:

  • Find a colleague and their workstation
  • View individual workstations in open spaces
  • View information about a colleague

2013: Start of the development. Solutions.

Onix team uses breaks between commercial projects to learn and test new technologies and create demos. Making an office map seemed both interesting and practical. At that time, everyone was discussing indoor navigation issues. We also considered several projects, one of which would employ iBeacon.

Thus, our work on the map began in July 2013. At that time, the project team

  • Drew in Photoshop the design of the premises in a pseudo-scale, several types of desks, and the boys and girls.
  • Used the Я.Карт engine to create the map. (This engine, which could be used separately on our server, enabled the developers to lock zoom, upload employee cards, and drag the map easily.)
  • Chose PHP as the back-end programming language.
  • Synchronized the employees’ data from the LDAP system.
  • Built a simple admin panel for the administrator to enter the employees’ workstations and arrange the desks of various types.

The map was only accessible via the office IP address and was closed to external users.

Even that simple, the office map performed its function, albeit with some difficulties. For example, someone had to monitor workstation changes and manually update the map accordingly. It also lacked advanced search, e.g., by skills and technologies, in addition to the first and last name.

Therefore, when Onix’s office space and personnel expanded, we felt it was time to update and improve the map.

2019: Version 2.0 

In the fall of 2019, having collected our colleagues’ requirements for a new map, we set to work.

In fact, at that time, the colleagues only wanted to:

  • add to the map the floors the company acquired
  • add employees’ photos to their cards
  • quickly copy a colleague’s email or phone number (because we mainly communicated via Telegram) or promptly open the dialogue window in Skype
  • add technologies and employee roles, such as Project Manager, Tech Lead, React Developer, etc.
  • conveniently search by technologies and skills

However, appetite comes with eating, and once software developers find themselves in a customer’s shoes, they begin to love changes to the original project scope. So, the project team ended up doing a bit more.

Meeting rooms and workstations

Before the pandemic, nearly all employees attended the office. And since their work often requires teleconferences with clients, the demand for meeting rooms was high. For colleagues to know which room is currently occupied and which is available, we decided to add the appropriate functionality to the map.

Firstly, we added real photos of the different rooms. The HR team painstakingly photographed each room in the office to give an idea of its size and capacity. Now it’s easy to determine which one of the 16 rooms is suitable for a specific conference with or without a team.

Secondly, we developed a room reservation functionality. All rooms were entered into a corporate Google account. When one reserves a specific time slot, one can choose one of the available rooms. A room can be booked not only from the calendar but also from its “profile” page. One should select “Reserve” on the room’s photo, and this option will promptly appear on the calendar.

Thirdly, we updated the map header to feature the quick room reservation for selected times. Users also can see when a room is available for reservation and when it is already occupied or booked.

It is also possible to reserve workstations in the office. This is helpful for colleagues who work remotely but come to the office occasionally and in the case of height-adjustable desks, which are still few in the office. The office map thus enables employees to reserve some of the workstations in advance.

Safety of the office and people

The best way to show fire extinguishers’ locations is to mark them on a map. I confess I never paid attention to them, where they hang, and how many. So we organized a mini-quest “find all the fire extinguishers in the office.” The same about first aid kits.

I doubt anyone will be looking for a fire extinguisher on the map in case of fire. However, if one views the map regularly, one involuntarily gets to remember where they are. It is also convenient to show new employees the fire extinguishers on the map during onboarding.

Locations

Office space is not only about work: there’s time and place for leisure with colleagues. Rumor has it that developers play Playstation at work… Let’s be honest: they do. They also play table football. Moreover, they play the guitar and bass guitar (yuck!), drink tons of coffee, eat ice cream in the lounges, and more.

There are many locations in our offices where you can catch them, but you won’t catch up with them if you never leave your workstation. Our office map helps familiarize new team members with Onix’s office; they can arrange a virtual tour and then go to see everything live.

Accessibility

Previously, the map was accessible only from the office IP address. However, during the pandemic, everyone worked remotely. By then, we had implemented a convenient search by technology and skills, levels of expertise (from novice to expert), and sorting by experience and birthday (we also send birthday greetings), so the office map became a popular instrument even outside the office.

So we integrated the map into Hydra, our company’s ERP, and implemented a common login. The map is now available to any colleague from anywhere in the world.

Workstations

A small office with some twenty desks hardly poses a numbering problem, not to mention the desk numbering logic. However, things changed with Onix’s office encompassing 8 floors and hundreds of workstations. 

Stickers provided a solution. We came up with a smart way to mark all desks on one floor and repeated the procedure 7 times. To make it clear, stickers with numbers were placed on the desks.

Additionally, we enabled employees to change their workstation numbers in their profiles. If they moved to another desk, they change its number themselves. This solved the administrator’s problem of tracking the employees and workstations and updating the map.

A colleague who works remotely can also be found on the map. Instead of the workstation number, they have the “Remote” mark. After all, the map should provide helpful information regardless of an employee’s location.

P.S. Occasionally, e.g., on April 1, we have cats sitting, lying, and working at our desks! And birthday boys and girls have festive profile pictures with air balloons, confetti, and party hats.

Summing up

Our digital office map has long outgrown its navigation function. The best way to see it is to compare the initial requirements with the results we have now:

  • A convenient service integrated with the company’s ERP Hydra, which can be independent if needed. (Here’s a demo.)
  • Integration with Google Calendar to reserve meeting rooms and height-adjustable desks.
  • Each employee can make changes to their profile (photo, email, social networks, etc.), including changing their workstation.
  • Access for all employees via single sign-on (SSO).
  • Custom solution based on SVG instead of Yandex.map.
  • The back-end is written the latest PHP and the interface on the Vue.js framework.
  • Pseudo-scaled design that conditionally renders the dimensions and coordinates of desks and other objects.
  • Clickable zones to indicate leisure locations and objects.
  • Adaptation for convenient use on mobile phones.
  • Updated headbar with an advanced search, locations, company services, and option to book a meeting room or workstation.
  • Sidebar showing the floors for left-handed and right-handed users.
  • Multiple languages: the map is available in English and our beloved mother tongue.
  • Balloons and party hats on profile pictures for everyone to know whose birthday it is.