Simple tests of speed of different iterations in JavaScript. Note that swapping the 1st and 2nd iframes, swaps the results as well, suggesting these two variations are identical in speed, but whichever one comes first ends up clocking slightly faster because of memory usage I guess. Clearly the third method is faster. Refresh the page to see more results.
Code used to generate these tests.
function init() { var start = new Date(); for (var i = 0; i < 10000000; i++) { } var end = new Date(); var time = end - start; var first = document.getElementById("first"); first.innerHTML = "(var i = 0; i < 10000000; i++) => " + time + " ms."; } window.onload = function () { init(); } |
function init() { var start = new Date(); for (var i = 1; i <= 10000001; i++) { } var end = new Date(); var time = end - start; var second = document.getElementById("second"); second.innerHTML = "(var i = 1; i <= 10000001; i++) => " + time + " ms."; } window.onload = function () { init(); } |
function init() { var start = new Date(); for (var i = 10000001; i >= 1; i--) { } var end = new Date(); var time = end - start; var third = document.getElementById("third"); third.innerHTML = "(var i = 10000001; i >= 1; i--) => " + time + " ms."; } window.onload = function () { init(); } |