array-iteration

JavaScript performance comparison

Test case created by Bulat

Info

Loops vs. Array methods vs. libraries.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var array = [1, 3, 15, 28, 1, 243, 45, 12, 34, 56, 23, 6, 34, 345345, 345, 1245, 576, 9, 902, 9930, 8, 37, 85, 12];
   
    function isEven(number) {
        return number % 2 === 0;
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Basic uncached loop
for (var i = 0; i < array.length; i++) {
    isEven(array[i]);
}
pending…
Native Array.forEach()
array.forEach(function(value) {
    isEven(value);
});
pending…
jQuery $.each()
$.each(array, function(_, value) {
    isEven(value);
});
pending…
Length-cached for-loop
for (var i = 0, len = array.length; i < len; i++) {
    isEven(array[i]);
}
pending…
Foreach loop (bad bad bad!)
for (var index in array) {
    isEven(array[index]);
}
pending…

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

Compare results of other browsers

0 comments

Add a comment