Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda