Object Creation

JavaScript performance comparison

Test case created by adrianmcli

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let myClassObject;
  let myFuncObject1;
  let myFuncObject2;
  
  // using the `class` keyword
  class MyClassObject {
    constructor(initVal) {
      this.myVal = initVal;
    }
  
    set(x) {
      this.myVal = x;
    }
  }
  
  // using a function with `this`
  const makeObject1 = initVal => {
    return {
      myVal: initVal,
      set: function(x) {
        this.myVal = x;
      },
    };
  };
  
  // using a function without `this`
  const makeObject2 = initVal => {
    let myVal = initVal;
    return {
      get: function() {
        return myVal;
      },
      set: function(val) {
        myVal = val;
      },
    };
  };

};

Benchmark.prototype.teardown = function() {
  delete myClassObject;
  delete myFuncObject1;
  delete myFuncObject2;

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
ES2015 Classes
myClassObject = new MyClassObject(0)
pending…
Function with this
myFuncObject1 = makeObject1(0)
pending…
Function without this
myFuncObject2 = makeObject2(0)
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