reused var in for-loop

JavaScript performance comparison

Test case created by Brendan Baldwin

Info

Just want to see if reassignment of a var declared once in the for () stanza beats out an explicit var declaration and assignment in the loop body.

Preparation code

<script>
var numbers = [];
for (var n = 0; n < 1000; ++n) {
  numbers[n] = n;
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
var in body
for (var n = 0, l = numbers.length; n < l; ++n) {
  var number = numbers[n];
}
pending…
reused var in for
for (var n = 0, l = numbers.length, number; n < l; ++n) {
  number = numbers[n];
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment