Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Created July 6, 2011 03:33
Show Gist options
  • Select an option

  • Save eliperelman/1066503 to your computer and use it in GitHub Desktop.

Select an option

Save eliperelman/1066503 to your computer and use it in GitHub Desktop.
Speedy Loops
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]);
}
@eliperelman

Copy link
Copy Markdown
Author

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

@atk

atk commented Jul 7, 2011

Copy link
Copy Markdown

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