Accessing Property Chains

JavaScript performance comparison

Test case created

Preparation code

<script>
  var valid = {
    foo: {
      bar: {
        baz: "Value"
      }
    }
  };
  var invalid = {
    foo: {}
  };
  var value;

  function get(val, names) {
    while (val && names.length) {
      val = val[names.shift()];
    }
    return val;
  }
</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
Try/Catch (property exists)
try {
  value = valid.foo.bar.baz;
} catch (e) {}
pending…
Try/Catch (property doesn't exist)
try {
  value = invalid.foo.bar.baz;
} catch (e) {}
pending…
&& chain (Property exists)
value = valid && valid.foo && valid.foo.bar && valid.foo.bar.baz;
pending…
&& chain (Property doesn't exist)
value = invalid && invalid.foo && invalid.foo.bar && invalid.foo.bar.baz;
pending…
Helper function (Property exists)
value = get(valid, ["foo", "bar", "baz"]);
pending…
Helper function (Property doesn't exist)
value = get(invalid, ["foo", "bar", "baz"]);
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