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

141
Visualizações
How to improve forEach in JavaScript for large lists

I have a pretty big data object and every time I refresh the page I have to resume the data from that water. Is there a way I can make a forEach faster?

if (Object.keys(jsonData.data.users).length > 0) {
  Object.keys(jsonData.data.users).forEach((username) => {
    const user = jsonData.data.users[username];
    const register_date = new Date(user.register_date);

    if (register_date >= start_date && register_date <= end_date) {
      data.guestsNumber++;
    }

    actions += user.actions_per_session;

    session_duration += user.session_duration;

    data.cartViews += user.cart_views;
  })

  data.actionsPerSession = (actions / (Object.keys(jsonData.data.users).length)).toFixed(2);

  data.averageSessionDuration = (session_duration / (Object.keys(jsonData.data.users).length)).toFixed(2);
}

What ways would there be to give a better time?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In general, this looks sufficiently fast. You can take some benchmarks via jsbench.me, but I think you're going to have to look beyond the code to re-architecting overall page load times.


But here's how I'd refactor the code to squeeze out performance

You can convert if (arr.length > 0) { arr.forEach(...) } to just do arr.forEach(...). If the array is empty, forEach will just never fire the callback, but is still valid. That prevents doing Object.keys(jsonData.data.users) twice.

Also, you're calling Object.keys(jsonData.data.users) 4 different times; should be slightly faster to allocate to a variable and re-use that.

So should look like this:

var users = Object.keys(jsonData.data.users);
users.forEach((username) => {
    const user = jsonData.data.users[username];
    const register_date = new Date(user.register_date);

    if (register_date >= start_date && register_date <= end_date) {
      data.guestsNumber++;
    }

    actions += user.actions_per_session;

    session_duration += user.session_duration;

    data.cartViews += user.cart_views;
})

data.actionsPerSession = (actions / (users.length)).toFixed(2);
data.averageSessionDuration = (session_duration / (users.length)).toFixed(2);
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