# Boost Node.JS  performance by Promise Pool

The async function, which is at the core of JavaScript, is constantly utilized to improve the user experience. As JavaScript evolved, [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) were introduced to alleviate the burden of managing callback functions when an application handles a lot of async functions concurrently.

Promise Pool is a concept to handle numerous concurrent operations more effectively in the progression of async functions.

# Let's begin with a real-world use scenario.

Consider the scenario when we need to add a lot of products to the database from a CSV file or any other external API source.

The following code snippet represents one possible implementation.

%[https://gist.github.com/priyankatgit/4d64723d36b3657686fa4b2ce0c45fbb] 

However, a seasoned developer can make the observation and will say - *"Hey dude, your code is inefficient because it waits for each import product process sequentially, thus overall product import process would take much more time."* 👎

That's correct, so let's use the promise all feature to make it better so that each product import process can run concurrently and the whole import process will be incredibly quick.

%[https://gist.github.com/priyankatgit/850ad430e00916cebf5779c2ffb6b24e] 

## But there are always drawbacks to advantages. 🤔

Now imagine that you are importing thousands of products. If you were to use `promise.all()` in that scenario, you would end up running thousands of operations concurrently, which would result in a locking involved database tables, high CPU usage on the server, a backlog of requests for the V8 engine to process, as well as other problems.

Promise pools libraries can help in this situation. The idea is straightforward; it gives us the ability to manage multiple operations at once. The same principle is shared by many libraries, however in this article I'll focus on the [Supercharge Promise Pool](https://github.com/supercharge/promise-pool) library.

%[https://gist.github.com/priyankatgit/a04ace9f04d691e007afd249a7f6313f] 

With its `withConcurrency` API, we can define any positive arbitrary number, and it will carry out that many operations concurrently at any given time.

The `process` function takes a callback function where any processing function can be passed and promises are resolved based on the defined concurrent operation capacity.

### Conclusion

While both the Promise.all() and Promise Pool libraries can run several processes concurrently, the Promise Pool library is better if there is a larger amount of data to be processed because it has the advantage of providing API control over the parallel process.
