Created
July 6, 2011 03:33
-
-
Save eliperelman/1066503 to your computer and use it in GitHub Desktop.
Speedy Loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = ['apple', 'banana', 'cheese', 'donut', 'egg', 'fish']; | |
var send = function (item) { | |
$.ajax({ | |
type: 'post', | |
dataType: 'json', | |
url: '/Foods/Consume', | |
data: { food: item } | |
}); | |
}; | |
// Normal for loop: | |
for (var i = 0, len = arr.length; i < len; i++) { | |
send(arr[i]); | |
} | |
// Faster reverse while loop: | |
var i = arr.length; | |
while (i--) { | |
send(arr[i]); | |
} |
I would guess the for loop in reverse being slower than the while loop in reverse is due to the testing conditions, which are rather unprecise.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Understood, but the purpose of this post is to note that the reverse while is the fastest performing JS loop: http://blogs.oracle.com/greimer/entry/best_way_to_code_a