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

204
Visualizações
How to benchmark react component?

I want to benchmark rendering of a React component so that I could make incremental improvements.

I came up with this prototype for use with react v18:

import {
  useEffect,
} from 'react';
import {
  createRoot,
} from 'react-dom';
import {
  JsonView,
} from '../components/JsonView';

const render = (root) => {
  return new Promise((resolve) => {
    root.render(<div ref={() => {
      resolve(undefined);
    }}
    >
      <JsonView
        activePath={null}
        entries={{foo: 'bar'}}
        expanded
        highlights={[]}
        onActivePathChange={() => {}}
      />
    </div>);
  });
};

const TestPage = () => {
  useEffect(() => {
    const sandbox = document.createElement('div');

    document.body.appendChild(sandbox);

    const root = createRoot(sandbox);

    (async () => {
      const startTime = Date.now();
      const runTime = 1_000;

      let renderCount = 0;

      while (Date.now() - startTime < runTime) {
        await render(root);

        renderCount++;
      }

      console.log('done', renderCount);
    })();

    return () => {
      document.body.removeChild(sandbox);
    };
  }, []);

  return <div />;
};

export default TestPage;
  • JsonView is the component that I want to benchmark.
  • ref is used to identify when div is created.
  • startTime / runTime are used to limit test suite run time.

This works well – I am getting consistent benchmark outputs and making changes to the component shows performance improvement / degradation.

However, I was wondering if this is the right way to benchmark a React component or if I should be using another mechanism?

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