Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda