Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery map vs Javascript map vs For-loop

I'm implementing some code that is a natural fit for map. However, I have a significant amount of objects in a list that I'm going to iterate through, so my question is which is the best way to go abou this:

var stuff = $.map(listOfMyObjects, someFunction())

var stuff = listOfMyObjects.map(someFunction())

or just

var stuff = new Array(); 
for(var i = 0; i < listOfmyObjects.length; i++){
    stuff.push(someFunction(listOfMyObjects[i]));
}
like image 781
dave Avatar asked Jul 01 '11 17:07

dave


People also ask

Is map better than for loop JS?

Comparing performance , map() wins! map() works way faster than for loop.

Which is faster for loop or map in JavaScript?

Even with these simple tests, loops are almost three times faster.

Which loop is faster in jQuery?

each() loop : Lower performance compare to other loops (for games/animations/large datasets) Less control over iterator (skip items, splice items from list, etc). Dependency on jQuery library unlike for, while etc loops!

Is filter faster than for loop JavaScript?

To our surprise, for-loops are much faster than the Array. filter method. To be precise, the Filter method is 77% slower than for loop.


1 Answers

here is a test case done in jsben.ch: http://jsben.ch/#/BQhED

it shows that a for-loop map is faster than a jquery map (at least in chrome).

like image 194
Socram Avatar answered Sep 28 '22 10:09

Socram