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

118
Visualizações
Jquery grep Vs each for better performance

I have a scenario to create two new arrays(based on certain conditions) from an existing array. I have two different ways to get my result.

  1. Use Jquery grep() twice to filter the original array.

         var arrErrorRecs = [], arrWarningRecs = [];
         arrErrorRecs = jQuery.grep(aSelectedRecs,function(oItem){
             return oItem.Stat === "Error";
         });
         arrWarningRecs = jQuery.grep(aSelectedRecs,function(oItem){
             return oItem.Stat === "Warning";
         });
    
  2. Use Jquery each() once and populate the two new arrays

        jQuery.each(aSelectedRecs, function(){
             if(this.MinPriceValidation === "Error"){
                 arrErrorRecs.push(this);
             }else if(this.MinPriceValidation === "Warning"){
                 arrWarningRecs.push(this);
             }
         });
    

I have an understanding that in this case Jquery.each would be best as it is looping the array only once. Please advise which is the most effective way for better performance.

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

0

Since both code is O(n), I don’t think there is much problem with grep, but sure if the length of array is very huge, each would likely to be faster. But I believe not on the crazy difference though.

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