object literal vs contructor

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var Construct = function(a, b) {
        this.a = a;
        this.b = b;
        this.c = a + b;
        }
       
       
       
       
       
       
       
    var literal = function(a, b) {
        return {
          a: a,
          b: b,
          c: a + b
        };
        }
       
       
       
       
       
       
       
    var items = [],
        i = 1000;
    while (i--) {
      items.push([Math.round(Math.random() * 1000000, 0).toString(16), Math.round(Math.random() * 1000000, 0).toString(16)])
    }
};
</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
construct
for (var i = 0; i < 1000; i++) {
  var a = new Construct(items[i][0], items[i][1]);
  a.d = a.a + a.b + a.c;
}
pending…
literal
for (var i = 0; i < 1000; i++) {
  var a = literal(items[i][0], items[i][1]);
  a.d = a.a + a.b + a.c;
}
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