title: ES6 classes performance
body: <p>Ok, basic thing that I've read: ES6 classes are just sugar syntax. Ok so I wanted to check it out on a performance level. I've create a script where I iterate over n times on an object which increment an internal counter.</p>
  
  <p>I've tried different ways, with <code>new</code> syntax, plain object, <code>Object.create</code> syntax and... ES6 <code>class</code> syntax.</p>
  
  <p>I've executed my scripts in node, v4 on windows, v5 and v6 on linux mint. Each time the results are the same:</p>
  
  <ol>
  <li>Objects with new or Object.create are faster</li>
  <li>Closurish methode take +/- twice the time</li>
  <li>ES6 class takes... <strong>10 times more time !!</strong></li>
  </ol>
  
  <p>Question now: are my tests relevants ? Am I doing something wrong on a syntax point of view ? OR ES6 classes are just really really slow ?</p>
  
  <p>Thank you for your help !</p>
  
  <p><strong>EDIT: I didn't use any transpiler for this code, just doing node myFile.js to execute this tests</strong></p>
  
  <p><strong>EDIT: here is the execution result on node 6.2.2</strong></p>
  
  <pre><code>closureAlgo : 894ms
  closureFunctionAlgo : 400ms
  plainObjectAlgo : 861ms
  plainObjectCreateAlgo : 336ms
  objectAlgo : 335ms
  classAlgo : 2684ms
  </code></pre>
  
  <p>Here is the code for testing:</p>
  
  <pre class="lang-js prettyprint-override"><code>'use strict';
  
  // easier to read this big number !
  const iterations = Math.pow(10, 8);
  let i;
  
  function perfNode (cb) {
  
      var begin = process.hrtime();
      cb();
      var diff = process.hrtime(begin);
  
      console.log(arguments[0].name + ' : ' +Math.round((diff[0]*1000) + (diff[1]/1000000)) + "ms");
  }
  
  function perfWeb (cb) {
      var begin = performance.now();
      cb();
      var end = performance.now();
  
      console.log(Math.round((end - begin)) + "ms");
  }
  
  const perf = typeof performance !== 'undefined' ? perfWeb : perfNode;
  
  function closureAlgo() {
      function calc () {
          var count = 0;
  
          return {
              increment : function () {
                  count++;
              }
          }
  
      }
  
      var calcImpl = calc();
  
      for(i= 0; i &lt; iterations; i++) {
          calcImpl.increment();
      }
  }
  
  function closureFunctionAlgo() {
      function calc () {
          var count = 0;
  
          return {
              increment : function () {
                  count++;
              }
          }
  
      }
  
      var calcImpl = Object.create(calc());
  
      for(i= 0; i &lt; iterations; i++) {
          calcImpl.increment();
      }
  }
  
  function plainObjectAlgo() {
  
      var calc = {
          count: 0,
          increment: function () {
              this.count++;
          }
      };
  
      for(i= 0; i &lt; iterations; i++) {
          calc.increment();
      }
  
  }
  
  function plainObjectCreateAlgo() {
  
      var calc = {
          count: 0,
          increment: function () {
              this.count++;
          }
      };
  
      var calcImpl = Object.create(calc);
  
      for(i= 0; i &lt; iterations; i++) {
          calcImpl.increment();
      }
  
  }
  
  function objectAlgo() {
      function Calc () {
          this.count = 0;
      }
  
      Calc.prototype.increment = function () {
          this.count++;
      }
  
      var calcImpl = new Calc();
  
      for(i= 0; i &lt; iterations; i++) {
          calcImpl.increment();
      }
  
  }
  
  function classAlgo () {
    class Calc {
          constructor() {
              this.count = 0;
          }
  
          increment () {
              this.count++;
          }
      }
  
      var calcImpl = new Calc();
  
      for(i= 0; i &lt; iterations; i++) {
          calcImpl.increment();
      }
  }
  
  perf(closureAlgo);
  perf(closureFunctionAlgo);
  perf(plainObjectAlgo);
  perf(plainObjectCreateAlgo);
  perf(objectAlgo);
  perf(classAlgo);
  </code></pre>