Javascript console.timeEnd() Method compare two timers

Introduction

Which is fastest, the for loop or the while loop?

View in separate window

<!DOCTYPE html>
<html>
<body>
<script>
var i;//w  ww  .  j  a v  a2  s .co m
console.time("test for loop");
for (i = 0; i < 1000000; i++) {
  // some code
}
console.timeEnd("test for loop");

i = 0;
console.time("test while loop");
while (i < 1000000) {
  i++
}
console.timeEnd("test while loop");

</script>
</body>
</html>



PreviousNext

Related