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

126
Vistas
Measure execution time of web worker

I would like to measure execution time of a webworker that computes factorization. The webworker is messaging live results and once computations are terminated, the webworker sends a 'finished' message. I used promise to do this in my react app.

useEffect(() => {
  const measureTimeStart = performance.now();
  (async () => {
    try {
      await new Promise(resolve => {
        const worker = new Worker(new URL('facto.js', import.meta.url));
        worker.postMessage(number);
        worker.onmessage = message => { //streaming live results 
          const { facto, status } = message.data;
          setResults({ ...facto }); //adding them to React state
          if (status === 'finished') {
            resolve();
            worker.terminate();
          };
        };
      });
    } catch (error) {
      console.log(error)
    }
  })();
  const measureTimeEnd = performance.now();
  setExecutionTime(Math.round((measureTimeEnd - measureTimeStart) * 1000) / 1000 + ' ms');
}, [number]); //number to factorize

However, performance.now() doesn't wait that the promise resolves and consequently gives me arbitrary timing. Why? thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

useEffect(() => {
  runWebWorker()
}, [number])

async function runWebWorker() {
  const start = performance.now()
  await ...
  const end = performance.now()
}

Alternatively, have your worker send performance data, which is more measurable and excludes the postMessage queue latency.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to wait for the promise to resolve before measuring the time. Either with finally or then

useEffect(() => {
  const measureTimeStart = performance.now();
  (async () => {
    try {
      await new Promise(resolve => {
        const worker = new Worker(new URL('facto.js', import.meta.url));
        worker.postMessage(number);
        worker.onmessage = message => { //streaming live results 
          const { facto, status } = message.data;
          setResults({ ...facto }); //adding them to React state
          if (status === 'finished') {
            resolve();
            worker.terminate();
          };
        };
      });
    } catch (error) {
      console.log(error)
    }
  })().finally(() => {
    const measureTimeEnd = performance.now();
    setExecutionTime(Math.round((measureTimeEnd - measureTimeStart) * 1000) / 1000 + ' ms');
  });
  
}, [number]); //number to factorize

about 4 years ago · Juan Pablo Isaza Denunciar
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