Loop Reverse

JavaScript performance comparison

Revision 3 of this test case created

Info

what is the best way to loop? Forwards? Backwards?

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var list = ['radiohead', 'jeff', 'sigur ros', 'hammock', 'eluvium', 'mono', 'mogwai', 'olafur', 'hello destiny', 'cool things', 'between the buried and me', 'this will destroy you'];
   
    var doStuff = function(item) {
     var words = item.split(' ');
     return words;
    };
};
</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
each
$.each(list, function(idx, member) {
 doStuff(member);
});
pending…
for++
for(i = 0, l = list.length; i < l; i++) {
 doStuff(list[i]);
}
pending…
for--
var l = list.length;

for(i = l; i > 0; i--) {
    doStuff(list[i - 1]);
}
pending…
while
var l = list.length;
var r = list.reverse();

while(l--) {
 doStuff(r[l])
}
pending…
for-- 2
var l = list.length;

for(i = l; i-- > 0;) {
    doStuff(list[i]);
}
pending…
for -- 3
for(i = list.length; i--;){
doStuff(list[i]);
}
pending…
for -- 4
var l = list.length;
for(i = l; i--;){
doStuff(list[i]);
}
pending…
for -- 5
var l = list.length;
for(l; l--;){
doStuff(list[l]);
}
pending…
6
var l = list.length;
for(; l--;){
doStuff(list[l]);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment