DEV Community

Make Your Jest Tests up to 20% Faster by Changing a Single Setting

Ivan Tanev on March 25, 2021

Originally posted on ivantanev.com TL;DR When you have Jest as your test runner, passing the --maxWorkers=50% option will make the te...
Collapse
 
ninofiliu profile image
Nino Filiu

Thanks for sharing, I didn't expect such results! Didn't know about hyperfine either, that's an instant download for me

Collapse
 
teekay profile image
TK

I tried running our CI tests with:

  • --maxWorkers=50%: got worse from 13:30min to 15min
  • --runInBand: got worse from 13:30min to 22min

Maybe I'm doing something wrong. Any insights?

Collapse
 
zachbryant profile image
Zach

I was looking at this for local development, and was surprised to see max-workers at 75% boosted a slow-running test from 30s to 10s. But then I put it back at 50% and it was still 10s, same without the option anyway. So that makes me wonder, why does jest speed up locally after a few runs? Even with no cache?

Collapse
 
vantanev profile image
Ivan Tanev

Because of local filesystem caches - instead of having to hit the hard drive, the OS will store file metadata (or sometimes whole files) in memory after they have been accessed.

To reliably measure performance, you must use something like hyperfine:

hyperfine 'npm test' -w 3 // Will first run 3 warmup runs to make sure caches are primed, and only then do timing runs.

Collapse
 
markrity profile image
Mark Davydov

What a great article.
Thank you for all the insights and demonstration of this hyperfine tool.

Collapse
 
fllprbt profile image
fllprbt (Alkis Giouv)

Thanks for the article, how did you benchmark --watch?

Collapse
 
kamilius profile image
Oleksandr Hutsulyak

You don't need to benchmark "--watch" as it's basically running your tests over and over again.
If you want to benchmark npm script, which uses watch, just pass --watchAll=false to disable watch for that script

Collapse
 
fllprbt profile image
fllprbt (Alkis Giouv)

Sure, I mean if that's adequate to measure runtime performance of watched tests I 'm fine.

Collapse
 
anshles profile image
Ansh Saini

This article is a godsend

Collapse
 
ashvin777 profile image
Ashvin Kumar Suthar

This is really useful. My machine was extremely slow while running the jest tests, but now it's much better and my tests are also running faster.

Collapse
 
yyyyaaa profile image
Ha Gia Phat

neat, thanks for the benchmarking, definitely speeds up my tests locally

Collapse
 
lb profile image
LB (Ben Johnston)

Super helpful article thank you.

Collapse
 
cfecherolle profile image
Cécile Fécherolle

Very useful, thank you! I liked the examples of configuration with CRA or with Jest alone. I saved ~20 seconds on a 65 seconds local test run, which is very appreciated. Will now test the CI version!

Collapse
 
saltfix profile image
SALTFIX TECNOLOGIA

Great, thanks!!! This save my life.

Collapse
 
casschin profile image
Cass

Hm, this didn't seem to work for me. I have a MBP with the M1 Max chip (10 cores) and after running hyperfine benchmark and the 10 workers was the fastest.