Poolifier as Node.js thread/cluster pool
Tags: node, javascript, performance, computerscience
Published: 2021-03-02 07:29:07

Back in the days there was no chance to have a custom thread pool in a Node.js application.
From Node.js version 10 ( experimental ) a great module was provided, the [worker_threads](https://nodejs.org/docs/latest-v14.x/api/worker_threads.html) module. 
Worker_threads module enables the use of threads that execute JavaScript in parallel.

## The Beginning
Since that I am familiar with Java programming language, I immediately started to look for thread pool implementations just like are present in [java](https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html).
There was only one small and not exhaustive thread pool at that time, so I started to implement [poolifier](https://github.com/pioardi/poolifier).
I choosed to implement two thread pools, a fixed-size thread pool and a dynamic-size thread pool so that users could tune and choose which one fits better for their applications.

## Growth of the poolifier project
Poolifier initially got some downloads and interest but when I really was not sharing to much about it, I found out that the number of downloads increased a lot, few months later users started to ask for new features and small fixes.

## Community
Some contributions started to come into the project, in particular two people have being ( and still now are ) much much active to improve the project adding new features and enhancements.
By the way, special thanks to [Shinigami92](https://github.com/Shinigami92) and [Jerome Benoit](https://github.com/jerome-benoit) to help on the project so much !!!

## Poolifier v2
All the commitment on the project resulted in a new fresh version with enhancements and new features.
**New features**
-Cluster pools( pools of processes) are now available
-New worker selection strategies available

**Enhancements**
- Performance enhancements
- Project re-written in typescript
- Enhancement on internal code and variable names
- Add static code analysis with Sonar, and changed coveralls in favor of Sonar, this also covers security
- Production code minification for production to increase performances
- Added unit test cases
- Bug fixing

## Benchmarks
While poolifier was growing, other people ( including some Node.js gurus ) implemented new thread pools, so currently there are more and more thread pool implementations.
Due to that we choosed to measure the performance on a CPU intensive task for each of the most used thread pools and those are our results.
We chose to use hyperfine because it allows to run isolated Node.js processes so that each pool does not impact each other.
External pools ( we choosed the most popular that we found ) with which we compared the poolifier results:
- [piscina](https://github.com/piscinajs/piscina)
- [SUCHMOKUO/node-worker-threads-pool](https://github.com/SUCHMOKUO/node-worker-threads-pool)
- [threads.js](https://github.com/andywer/threads.js)

Those are our results running for each pool a CPU intensive task 100k times :

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/222p2549rvjws0trv544.png)

More info on how to run this benchmark on your own are available [here](https://github.com/pioardi/poolifier/blob/master/benchmarks/README.md).

## Conclusion
So based on the benchmarks poolifier seems to be the fastest thread pool and well suited to run Node.js CPU intensive tasks and now it comes with fresh new features to try and an active community.
We will measure poolifier versus other pools with more and more task types ( that could fit better on processes pools instead of thread pools )going forward !!!
What you wait? Try poolifier! Leave a star, consider to contribute, ask a question !!!

Thanks to read this post, if you liked it please share.