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

119
Vistas
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 Respuestas
Responde la pregunta

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 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