For Loop (v73)

Revision 73 of this benchmark created by wfreytag on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
</script>
<script>
  var underscore = _.noConflict();
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
  var arr = underscore.range(1,1000);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i]=i;
  i++;
};
ready
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
  arr[i]=i;
  i++;
};
ready
Reverse while loop
var i = arr.length;
while (i--) {
  arr[i]=i;
};
ready
Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i-- > 0) {
  arr[i]=i;
};
ready
Reverse do … while loop
var i = arr.length;
do {
  arr[i]=i;
} while (i--);
ready
Reverse for loop
for (var i = arr.length; i--;) {
  arr[i]=i;
};
ready
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i]=i;
};
ready
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i]=i;
};
ready
Cool guy loop
for (var i = -1; ++i < arr.length;) {
  arr[i]=i;
};
ready
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i]=i;
};
ready
Native Array#forEach implementation
arr.forEach(function(x) {
  arr[x]=x;
});
ready
Native Array#forEach implementation with named function
function foo(x) {
  arr[x]=x;
};
arr.forEach(foo);
ready
Loop case 1
var i;
for (i = 0; i < arr.length; i += 1) {
  arr[i]=i;
}
ready
Loop case 2
var i, l;
for (i = 0, l = arr.length; i < l; i += 1) {
  arr[i]=i;
}
ready
jQuery each
$.each(arr,function(v){
arr[v]=v;
})
ready
Underscore Each
underscore.each(arr,function(v){
arr[v]=v;
})
ready
lodash each
lodash.each(arr,function(v){
arr[v]=v;
})
ready

Revisions

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