Mixing sequential and parallel promises
source link: https://www.tuicool.com/articles/hit/3EBZ3ue
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Mixing sequential and parallel promises
Preface and introduction may be important, but I would prefer to show you the code first.
The task is to lint list of files. As this list can grow beyond 100 files, it is challenging task to do in the most optimal way.
If I use simple Promise.all
I am risking putting the OS under too much pressure.
If I use sequential run of each lint command, that would be noticeably slow.
So I needed to mix both options for best result, i.e.running Promise.all
on sequential list of promises.
This is exactly what happens in the example.
The list of files is split to 5 quasi-equal parts with R.splitEvery
and each part is passed to R.fastAsync
, which uses Promise.all
underneath.
It is easy to make comparison between the described logic and sequential run of promises, if we change R.mapFastAsync
to R.mapAsync
.
With list of 203 files the results are:
- Sequential promises = 320 seconds
- Promises.all with chunks = 130 seconds
References:
rambdax — https://github.com/selfrefactor/rambdax
lintFolder method— https://github.com/selfrefactor/run-fn/blob/master/services/lintFolder/index.js
And the obligatory meme for thumbnail purpose:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK