Fast Array Foreach vs shift (v269)

Revision 269 of this benchmark created on


Description

check if shift with while loop is faster than foreach with indexing;

Setup

// Populate the base array
      var arr = [];
      for (var i = 0; i < 1000; i++) {
        arr[i] = i;
      }
    
      function someFn(i) {
        return i;
      }

Test runner

Ready to run.

Testing in
TestOps/sec
Array.ForEach
arr.forEach(someFn)
ready
For
for (var i = 0, len = arr.length; i < len; i++) {
  someFn(arr[i]);
}
ready
Shift-while
var index = 0;
for( var i = arr.shift(); i !== undefined; i = arr.shift()) {
   if (someFn(i) !== index) {throw Error('numbers dont match')}
   index = index + 1;
}
ready

Revisions

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