Getter vs dot notation

JavaScript performance comparison

Test case created by PaulH

Info

I want to detect typos in code, and get performance.

eg If I have obj.x = 2, I want to detect when I might accidentally type var y = obj.y; instead of y = obj.x;

ie I don't want javascript to allow me to read properties that don't exist.

http://stackoverflow.com/questions/13757631/how-to-prevent-access-to-object-properties-that-do-not-yet-exist

Preparation code

<script>
  var obj = {
    x: 2
  };

  var getter = function(name, check) {
      if (check && typeof(obj[name]) == "undefined") {
        throw "Undefined property " + name;
      }
      return obj[name];
      };

  var getter_with_in = function(name) {
      if (!(name in obj)) {
        throw "Undefined property " + name;
      }
      return obj[name];
      }
</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
dot notation
return obj.x;
pending…
getter
return getter('x', true);
pending…
getter, no checks
return getter('x', false);
pending…
getter using in instead of typeof
return getter_with_in('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