Declaring variables inside loops

JavaScript performance comparison

Revision 7 of this test case created by Terry Anastasiadis

Info

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

Preparation code

<script>
  var LENGTH = 3;
</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
Inside
var len_00 = LENGTH;
while (len_00-- > 0) {
  var len_01 = LENGTH,
      a = {},
      b = {},
      c = {},
      d = {},
      e = {},
      f = {};
  while (len_01-- > 0) {
    var len_02 = LENGTH,
        g = {},
        h = {},
        i = {},
        j = {},
        k = {},
        l = {};
    while (len_02-- > 0) {
      var len_03 = LENGTH,
          m = {},
          n = {},
          o = {},
          p = {},
          q = {},
          r = {};
      while (len_03-- > 0) {
        var s = {},
            t = {},
            u = {},
            v = {},
            w = {},
            x = {},
            y = {},
            z = {};
      }
    }
  }
}
pending…
Outside
var len_00 = LENGTH,
    len_01, len_02, len_03, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
while (len_00-- > 0) {
  len_01 = LENGTH;
  a = {};
  b = {};
  c = {};
  d = {};
  e = {};
  f = {};
  while (len_01-- > 0) {
    len_02 = LENGTH;
    g = {};
    h = {};
    i = {};
    j = {};
    k = {};
    l = {};
    while (len_02-- > 0) {
      len_03 = LENGTH;
      m = {};
      n = {};
      o = {};
      p = {};
      q = {};
      r = {};
      while (len_03-- > 0) {
        s = {};
        t = {};
        u = {};
        v = {};
        w = {};
        x = {};
        y = {};
        z = {};
      }
    }
  }
}
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