Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!