Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Breaking long JS calculations up into pieces - strange Chrome behaviour

I'm working on a JS library that can potentially perform long computations, freezing the main thread. I know about Web Workers and they will be used as well, but I would like to ensure that the code can also be executed on the main thread without blocking it.

I've tried the following solution to break long computations up into smaller pieces:

const performLongComputations = () => {
    let sum = 0;
    for (let i = 0; i < 10_0000; ++i) {
        sum += Math.random();
    }
};

const synchronousExecution = () => {
    let totalTime = 0;
    for (let i = 0; i < 1_000; ++i) {
        const startTime = performance.now();
        performLongComputations();
        totalTime += performance.now() - startTime;
    }
    return totalTime;
};

console.log(`Synchronous execution took ${synchronousExecution()} ms`);

const asynchronousExecution = () => new Promise(resolve => {
    let totalTime = 0;
    let currentIteration = 0;

    const start = () => {
        const startTime = performance.now();
        performLongComputations();
        totalTime += performance.now() - startTime;

        if (currentIteration++ < 1_000) {
            setTimeout(start, 0);
        } else {
            resolve(totalTime);
        }
    };

    start();
});

asynchronousExecution().then(time => console.log(`Asynchronous execution took ${time} ms`));

In Chrome, the output is as follows:

Synchronous execution took 837.0999999996275 ms
Asynchronous execution took 2853.699999993667 ms

In Firefox:

Synchronous execution took 9674 ms
Asynchronous execution took 9203 ms

Tested on Linux, Chrome 92.0.4515.107, Firefox 90.0.

There are two things that surprise me:

  1. Why such a difference between sync and async code execution times in Chrome?
  2. Why is asynchronous code usually faster on FF?
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!