literal obj vs function obj

JavaScript performance comparison

Test case created by Nucking

Preparation code

<script>
  var A = {
      aa : function(){
          var i, j=[];
          var arr = ['Literal', 'Function'];
          for (i = 0; i < arr.length; i++){
              j[i] = arr[i];
          }
        return j[0];
      }
  };
  var A1 = A;
  var A2 = A1;
  A1.foo = ' Test';
  alert(A1.aa() + A2.foo);
 
  function B(){
      this.bb = function(){
          var i, j=[];
          var arr = ['Literal', 'Function'];
          for (i = 0; i < arr.length; i++){
              j[i] = arr[i];
          }
        return j[1];
      }
  }
 
  var B1 = new B();
  var B2 = new B();
  B.prototype.foo = ' Test';
  alert(B1.bb() + B2.foo);
</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
Literal
var A = {
    aa : function(){
        var i, j=[];
        var arr = ['Literal', 'Function'];
        for (i = 0; i < arr.length; i++){
            j[i] = arr[i];
        }
        return j[0];
    }
};
var A1 = A;
var A2 = A1;
A1.foo = ' Test';
alert(A1.aa() + A2.foo);
pending…
Function
function B(){
    this.bb = function(){
        var i, j=[];
        var arr = ['Literal', 'Function'];
        for (i = 0; i < arr.length; i++){
            j[i] = arr[i];
        }
        return j[1];
    }
}

var B1 = new B();
var B2 = new B();
B.prototype.foo = ' Test';
alert(B1.bb() + B2.foo);
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