$.each vs for in (v2)

Revision 2 of this benchmark created by Zeb on


Description

Purpose of this test case is to test the performance difference between using jQuery.each or using the original javascript 'for in' construct. JavaScript's 'for in' also loops through prototype properties of the object being iterated through whereas $.each doesn't.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  Array.prototype.filter_0 = function() {
   var res = [];
   for (var i = 0; i < this.length; i++) {
    if (this[i] != 0) {
     res.push(this[i]);
    }
   }
   return res;
  };
  var listeners = ["a", "b", "c"];
  var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  var x = 0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for in
for (o in arr) {
 x += o
}
ready
$.each (don't know how to fix)
$.each(listeners, function(key) {
 x++
});
ready
for in only named elems
for (o in arr) {
 if (listeners.hasOwnProperty(o)) {
  x += o
 }
}
ready
for i=0
var l = arr.length;
for (var i = 0; i < l; i++) {
 x += arr[i];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.