Nodejs performance tips
Nodejs performance tips.
How many threads does nodejs use by default
In a typical Node.js process, here's a breakdown of the threads used:
-
Main Thread: This is the single thread that runs the Node.js event loop and executes JavaScript code.
-
Libuv Thread Pool: By default,
libuv
creates a thread pool with 4 threads. These threads are used for handling asynchronous I/O operations, such as file system tasks and DNS lookups. -
V8 Engine Threads: Node.js uses the V8 JavaScript engine, which itself utilizes additional threads. Typically, there are 2 threads used by V8 for tasks such as garbage collection and code optimization.
How to increase the number of threads for libuv
thread pool
To increase the thread pool size in Node.js by libuv
, set the UV_THREADPOOL_SIZE
environment variable:
UV_THREADPOOL_SIZE=64 node app.js
-
The maximum value for
UV_THREADPOOL_SIZE
can be1024
, and it's generally recommended to set it to the number of logical cores to optimize performance for I/O-bound tasks. Setting it too high without justification can lead to increased overhead and potential inefficiencies. -
In cluster setup, each worker process will have its own thread pool, the total number of threads for the thread pool will be
UV_THREADPOOL_SIZE * numberOfWorkers
. -
For CPU-bound tasks, increasing
UV_THREADPOOL_SIZE
may not yield significant benefits and could lead to resource contention. Leverage thecluster
orworker_threads
module instead.
How to tune or improve memory performance
Leverage the --expose-gc
option
- The
--expose-gc
flag in Node.js is used to expose the garbage collection (GC) function, allowing to manually trigger garbage collection.
node --expose-gc app.js
# call global.gc() in code to request garbage collection
- Triggering garbage collection manually can pause the execution of the application, as all other operations are halted during GC. This can impact performance if used excessively. It should be used sparingly and only in specific cases where it is beneficial.
Tune the --max-old-space-size
value
-
The
--max-old-space-size
flag in Node.js is used to set the maximum memory size for the V8 engine's old generation heap, which is where long-lived objects are stored. -
The value for
--max-old-space-size
should be based on the available system memory and the specific needs of the application. For instance, if the server has 8 GB of RAM, set the flag to 6 GB to leave room for other processes and avoid swapping.
node --max-old-space-size=6144 app.js
# It can also be set with environment variable like:
export NODE_OPTIONS="--max-old-space-size=6144"