Nodejs Runtime Performance Tips

Rate this content

Nodejs runs big systems today. Sometimes, you can improve user experience and save on cloud expanses optimizing your nodejs scripts. In this talk I will share tips from production on how to improve nodejs runtime performance.

9 min
24 Jun, 2021

AI Generated Video Summary

This Talk focuses on the importance of runtime optimization in software development. It discusses the impact of unoptimized functions and the role of garbage collection. The Talk also highlights the use of profiling tools to identify and improve performance issues. Additionally, it emphasizes the importance of memory profiling to prevent memory leaks and optimize application performance.

1. Introduction to Runtime Optimization

Short description:

Many years ago, I encountered a critical issue in a big system where a stuttering microservice caused delays of 2-3 seconds. As a software architect at Vonage, I have dedicated years to optimizing runtime techniques. In this part, we will explore how to identify and improve the performance of unoptimized functions, using a simple example. We will also discuss the impact of garbage collection and the importance of runtime optimization.

Many years ago, I was working on a big system, a critical life and death kind of system. The system was working well, until one day I got a call from a customer. Yonatan, he says, the system is not answering my calls. What he was experiencing was a stuttering micro service in the system. There was one unoptimized function in the pipeline that was taking 2-3 seconds to finish instead of milliseconds. For my customer, these 2-3 seconds were critical, especially when looking at a system at scale.

My name is Yonatan Kra, software architect at Vonage. I'm an egghead instructor, blogger, and a full-time geek. I also enjoy running. I have spent years optimizing my runtime techniques, which is important when you need to run away from bullies. Today I'm going to show you how to spot unoptimized functions, and how to improve runtime performance in your applications.

We'll begin by looking at a simple example. We have two functions here that do the same. They both create an array and push elements into the array. BuildArray1 creates an empty array and dynamically pushes the indices into the array. BuildArray2 pre-allocates the array and just sets the right value in the right index. The result is the same. Let's take a look at their profile. In their profile, you can see that BuildArray1 took around 40 milliseconds to run, while BuildArray2 took around 7 milliseconds to run. That's a huge difference for functions that do the same thing. If we look deeper, we'll be able to see that BuildArray1 had lots of gray bars inside and these are garbage collection instances, while BuildArray2 did not have these garbage collection instances. So this is how we use profiling in order to compare performance of different implementations of the same functions, for instance. So we can see if improvements really improved our applications or search for the right solution.

A word about garbage collection. Garbage collection is the process in which JavaScript engines clear the memory from unreferenced object. I already said I'm a runner, and this is a good reason why I like to run on Saturdays because there are no garbage collectors on Saturday. Why should you care, actually, about runtime optimization? This is a caricature of the event loop in OJS. What you should take from this is that the event loop is running your main thread and it's running tasks. Tasks are actually the callbacks, your functions. And if a task takes longer, it blocks the main thread and during this runtime of the function, nothing else is running.

2. Optimizing Functions and Profiling Performance

Short description:

When optimizing your functions, it's important to ensure smooth running and prevent delays that can affect API calls and promises. Profiling tools like Chrome inspect page and dedicated dev tools for node can help identify and improve performance. Memory profiling can also uncover memory leaks and provide solutions. By clearing referencing arrays and monitoring allocations, you can prevent memory leaks and optimize your application's performance.

So if this function is running, no API calls are being taken care of, no promises are being resolved, and your application is just stuck and everything else is waiting. So this is a good reason to optimize your functions and make sure everything is running smoothly.

Let's see an example for that. For this, we'll go to the IDE. Look at this function, something quite noticeable. You should be familiar, it's an array that a million elements are being pushed to and we've added a set interval that makes sure it's being called once a second.

So if we run our node with the dash-dash inspect flag, sorry, it starts node in debug mode and we could go to the Chrome inspect page, open dedicated dev tools for node, and go to the profiler page. This time, we'll start recording for like two, three seconds, finish, and here we go. We have our intervals, one per second, and if you dip dive into it, we actually see our call to something quite noticeable and exactly how long it took the function to run. This way, we can profile everything in our node application. For instance, you can start an API call using Postman and track everything that happens from the moment the API call gets to the server until it just runs out and you can see how long it took every function to run and if you see a long running function, you might want to optimize this function.

Another issue in runtime performance is memory and we'll soon see how to profile memory and maybe even solve memory links. In this time, something quite noticeable, it pushes into an array that is created outside, so on every interval, we just increase this array and we do not garbage collect the internal array we had before. So, let's see how this looks when we profile memory.

So, I start again in debug mode. This time I'll go to the memory tab and I make sure allocation instrumentation on timeline is working. I start recording and I see these blue bars appear. A blue bar means a memory that was allocated and was not garbage collected. And if I focus on one of them, I actually see these 100 specials as expected. On every second 100 specials were allocated and not cleared. If I focus on one of the specials, I can see it was its index in the array, the name of the referencing array, and even the line in the code that allocated this object. I can easily see if I want this object to be garbage collected or not. If I do want it to be garbage collected, then I have a leak.

Let's fix this leak quite easily by just clearing the referencing array on every interval. I'll stop the server, restart it, and let's record again. Now, the blue bar becomes gray. Gray means that we had an allocation here, but it was garbage collected. Again, you can call your API, see if you have a blue bar that becomes gray after the API finish running, and if not, it might have a memory leak in your API handler, for instance.

Usually, when I speak to developers about performance or help developers solve performance issues, I see a lot of confusion in regards to how to handle performance problems. I hope this talk helped you understand the profiling tools that you have, the powerful profiling tools that you have, and how you can help your functions run faster and prevent memory leaks in your applications. I truly hope I piqued your interest to learn more about this subject. I'm very passionate about it, and to enjoy it as much as I do.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
React 18! Concurrent features! You might’ve already tried the new APIs like useTransition, or you might’ve just heard of them. But do you know how React 18 achieves the performance wins it brings with itself? In this talk, let’s peek under the hood of React 18’s performance features: - How React 18 lowers the time your page stays frozen (aka TBT) - What exactly happens in the main thread when you run useTransition() - What’s the catch with the improvements (there’s no free cake!), and why Vue.js and Preact straight refused to ship anything similar
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Our understanding of performance & user-experience has heavily evolved over the years. Web Developer Tooling needs to similarly evolve to make sure it is user-centric, actionable and contextual where modern experiences are concerned. In this talk, Addy will walk you through Chrome and others have been thinking about this problem and what updates they've been making to performance tools to lower the friction for building great experiences on the web.
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Do you know what’s really going on in your node_modules folder? Software supply chain attacks have exploded over the past 12 months and they’re only accelerating in 2022 and beyond. We’ll dive into examples of recent supply chain attacks and what concrete steps you can take to protect your team from this emerging threat.
You can check the slides for Feross' talk here.

Workshops on related topic

React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Featured WorkshopFree
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js ( Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow ( 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.