Javascript console.time() Method compare result

Introduction

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

Press F12 on your keyboard to view the result in the console view.

View in separate window

<!DOCTYPE html>
<html>
<body>
<script>
var i;//from  w  ww. j  a v  a  2  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