Declaring variables inside loops

JavaScript performance comparison

Revision 6 of this test case created

Info

Tests whether declaring a variable inside a loop, rather than declaring outside and just assigning values, is more performant

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Declare in a loop with one statement
var total = 0;
for (var i = 0; i < 1000; i++) {
  var x = 1, y = 2, z = {}, a = {};
  total += x;
}
pending…
Declare outside loop with one statement
var x, y, z, a, i, total = 0;
for (i = 0; i < 1000; i++) {
  x = 1;
  y = 2;
  z = {};
  a = {};
  total += x;
}
pending…
Declare in a loop with multiple statements
var i = 0;
var total = 0;
for (i = 0; i < 1000; i++) {
  var x = 1;
  var y = 2;
  var z = {};
  var a = {};
  total += x;
}
pending…
Declare outside loop with multiple statements
var x;
var y;
var z;
var a;
var i, total = 0;
for (i = 0; i < 1000; i++) {
  x = 1;
  y = 2;
  z = {};
  a = {};
  total += x;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment