by Maciej Cieślar

Limiting concurrent operations in JavaScript

Photo by Ales Nesetril on Unsplash

Usually, the machine that executes our code has limited resources. Doing everything at once might not only hurt, but can also hang our process and make it stop responding altogether.

When we want to crawl 100 websites, we should crawl, for example, 5 at once, so that we don’t take up all the available bandwidth. As soon as one website is crawled, the next one is ready to go.

Generally speaking, all “heavy” operations should be laid out in time. They should not be executed all-at-once, for better performance and to save resources.

Implementation

If you are familiar with my previous post about implementing promises, then you are going to notice many similarities.

We register a given task by adding it to our tasksQueue and then we call executeTasks.

Now we execute as many tasks as our limit allows us — one by one. Each time adding 1 to our counter called tasksActiveCount.

When the executed task finishes, we remove 1 from tasksActiveCount and again call executeTasks.

Below we can see an example of how it works.

The limit is set to 3. The first two tasks are taking very long to process. We can see the third “slot” getting opened from time to time, allowing the next task in the queue to be executed.

Always there are three, no more, no less.

Executing heavy and light tasks with the limit of 3.

You can see the code in the repository.

Thank you very much for reading! Can you think of any other way of achieving the same effect? Share them down below.

If you have any questions or comments feel free to put them in the comment section below or send me a message.

Check out my social media!

Join my newsletter!

Originally published at www.mcieslar.com on August 28, 2018.