while vs. for vs jquery each (v5)

Revision 5 of this benchmark created by GregWa on


Description

http://blogs.sun.com/greimer/entry/bestwaytocodea

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var longArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 40, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 60, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 70, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 90, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100];
  var shortArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
while Long Array
var res = 0;
var i = longArray.length
while (i--) {
 res += longArray[i];
}
ready
for Long Array
var res = 0
for (var i = longArray.length; i >= 0; --i) {
 res += longArray[i - 1];
}
ready
jquery each Long Array
var res = 0;
$.each(longArray, function() {
 res += this;
});
ready
for Alternative Long Array
var res = 0;
for (var i = longArray.length; i--;) {
 res += longArray[i];
}
ready

Revisions

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