JS Object Creation

JavaScript performance comparison

Revision 6 of this test case created

Info

Creating custom objects with hidden state and public mutators.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function ReadOnly(config) {
      for (var attr in config) {
        this[attr] = (function(attr) {
          return function() {
   
            return config[attr];
          };
        }(attr));
      }
    }
   
    function Getters(config) {
      this.age = function() {
   
        return config.age;
      };
   
      this.name = function() {
   
        return config.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
Getters
var me = new Getters({
  name: "Joshua",
  age: 33
});

me.name();
me.age();
pending…
Read Only
var me = new ReadOnly({
  name: "Joshua",
  age: 33
});

me.name();
me.age();
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