Angular VS Knockout VS Ember VS React VS Mithril

JavaScript performance comparison

Revision 542 of this test case created by Alex Mathews

Preparation code

<!-- Jquery -->
<script src="https://cdn.jsdelivr.net/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/angularjs/1.2.15/angular.min.js"></script>
<script src="https://cdn.jsdelivr.net/knockout/3.1.0/knockout.js"></script>
<script src="https://cdn.jsdelivr.net/handlebarsjs/1.3.0/handlebars.js"></script>
<script src="https://cdn.jsdelivr.net/emberjs/1.5.0/ember.min.js"></script>
<script src="https://cdn.jsdelivr.net/react/0.10.0/react-with-addons.js"></script>
<script src="https://cdn.jsdelivr.net/mithril/0.1.9/mithril.min.js"></script>


<!-- Angular -->
<div ng-app>
  Angular:
  <span ng-controller="Ctrl" id="angList"><span ng-repeat="item in data">{{item}}</span></span>
</div>


<!-- Knockout -->
<div id="koapp">
  Knockout:
  <span data-bind="foreach: data"><span data-bind="text: $data"></span></span>
</div>

<!-- Ember -->

<div id="emapp"></div>
<script type="text/x-handlebars">
  Ember:
  <span>
    {{#each EMapp.data}}<span>{{this}}</span>{{/each}}
  </span>
</script>


<!-- React -->
<div id="react">
  React: <span id="reactMountNode"></span>
</div>


<!-- Mithril -->
<div id="mithrilapp">
  Mithril: <span id="mithrilMountNode"></span>
</div>

<!-- X -->
<div id="xapp">
  X: <span id="xmount"></span>
</div>


<script>
var Ctrl = function($scope){ $scope.data = []; };

var KOData = ko.observableArray();
var KOviewmodel = {data: KOData};

var ENV = {EXTEND_PROTOTYPES: false};

var ReactComponent = React.createClass({displayName: 'PerfTest',
  getInitialState: function() {
    return { data: [] };
  },
  render: function() {
    var res = [];
    for(var i = 0; i < this.state.data.length; i++) {
      res.push(React.DOM.span({ key: 'test' + i }, this.state.data[i]));
    }
    return React.DOM.span(null, res);
  }
});

var MithrilData = new Array();
var mithapp = {
  controller: function() {
    this.data = MithrilData;
  },
  view: function(ctrl) {
    return m("span", [ctrl.data.map(function(datum) { return m('span', datum); })]);
  }
}

$(document).ready(function() {
  angular.element(document).ready(function() {
    var ang_scope = $('#angList').scope();

    window.ANGclear = function(){
      ang_scope.data.splice(0, ang_scope.data.length);
      ang_scope.$digest();
    };
    window.ANGpush = function(data){
      ang_scope.data.push(data + ang_scope.data.length);
      ang_scope.$digest();
    };
  });

  ko.applyBindings(KOviewmodel, document.getElementById('koapp'));
  window.KOclear = function (){
    KOData.splice(0, KOData().length);
  };
  window.KOpush = function (data){
    KOData.push(data);
  };



  EMapp = Ember.Application.create({
    rootElement: '#emapp'
  });
  EMapp.data = Ember.A();

  window.EMclear = function () {
    EMapp.data.clear();
  };
  window.EMpush = function (data) {
    EMapp.data.pushObject(data);
  };


  var reactComp = ReactComponent();
  React.renderComponent(reactComp, $('#reactMountNode')[0]);

  window.RClear = function() {
    reactComp.setState({data: []})
  };

  window.RPush = function(data) {
    var stateData = reactComp.state.data;
    stateData.push(data);
    reactComp.setState({data: stateData});
  }

  m.module(document.getElementById("mithrilMountNode"), mithapp);
  window.Mclear = function() {
    m.startComputation();
    MithrilData.splice(0);
    m.endComputation();
  };

  window.Mpush = function(data) {
    m.startComputation();
    MithrilData.push(data);
    m.endComputation();
  }
});
</script>

      
<script>
Benchmark.prototype.setup = function() {
  function is(thing, type){
      if(!thing) return false;
      thing = Object.prototype.toString.call(thing).slice(8,-1);
      return type ? (thing === type) : thing;
  }
  
  function el(ele){return document.getElementById(ele);}
  function t(tag, config){
      var parent = tag ? document.createElement(tag) : document.createDocumentFragment();
      if(config){
          _(Object.keys(config)).each(function(k){
              if(k === "class"){parent.classList.add(config[k]);}
              else if(k === "classes"){
                _(config[k]).each(function(e){parent.classList.add(e)})
              }
              else if(k === "click"){click(parent, config[k]);}
              else{parent[k] = config[k];}
          });
      }
      return function(ch){
          if(is(ch, "String") || is(ch, "Number")) parent.textContent = ch;
          if(!ch) return parent;
          if(is(ch, "Array")) _(ch).each(function(e){parent.appendChild(e)});
          if(is(ch).substr(0,4) === "HTML") parent.appendChild(ch);
          return parent;
      };
  }
  function nest(parent, child){
      while(parent.firstChild) parent.removeChild(parent.firstChild);
      parent.appendChild(child);
  }
  var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  
  var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
  
  var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };
  
  var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
  
  function _(array, at) {
      return new Aus(array, at);
  }
  var Aus = (function () {
      function Aus(array, at) {
          _classCallCheck(this, Aus);
  
          this.array = array;this.at = at || 0;
      }
  
      _prototypeProperties(Aus, null, {
          map: {
              value: function map(fn) {
                  var result = [],
                      index = 0,
                      i = this.at,
                      j = this.array.length;
                  while (i < j) {
                      result[index++] = fn ? fn(this.array[i], i) : this.array[i];
                      i++;
                  }
                  return result;
              },
              writable: true,
              configurable: true
          },
          filter: {
              value: function filter(fn) {
                  var result = [],
                      index = 0,
                      i = this.at,
                      j = this.array.length;
                  while (i < j) {
                      if (fn(this.array[i])) result[index++] = this.array[i];
                      i++;
                  }
                  return result;
              },
              writable: true,
              configurable: true
          },
          each: {
              value: function each(fn) {
                  var i = this.at,
                      j = this.array.length;
                  while (i < j) {
                      fn(this.array[i], i);i++;
                  }
              },
              writable: true,
              configurable: true
          },
          reduce: {
              value: function reduce(fn) {
                  var i = this.at + 1,
                      j = this.array.length,
                      value = this.array[this.at];
                  while (i < j) value = fn(value, this.array[i++]);
                  return value;
              },
              writable: true,
              configurable: true
          }
      });
  
      return Aus;
  })();

};
</script>

Preparation code output

<!-- Jquery --> <script src="https://cdn.jsdelivr.net/angularjs/1.2.15/angular.min.js"></script> <script src="https://cdn.jsdelivr.net/knockout/3.1.0/knockout.js"></script> <script src="https://cdn.jsdelivr.net/handlebarsjs/1.3.0/handlebars.js"></script> <script src="https://cdn.jsdelivr.net/emberjs/1.5.0/ember.min.js"></script> <script src="https://cdn.jsdelivr.net/react/0.10.0/react-with-addons.js"></script> <script src="https://cdn.jsdelivr.net/mithril/0.1.9/mithril.min.js"></script> <!-- Angular --> <div ng-app> Angular: <span ng-controller="Ctrl" id="angList"><span ng-repeat="item in data">{{item}}</span></span> </div> <!-- Knockout --> <div id="koapp"> Knockout: <span data-bind="foreach: data"><span data-bind="text: $data"></span></span> </div> <!-- Ember --> <div id="emapp"></div> <script type="text/x-handlebars"> Ember: <span> {{#each EMapp.data}}<span>{{this}}</span>{{/each}} </span> </script> <!-- React --> <div id="react"> React: <span id="reactMountNode"></span> </div> <!-- Mithril --> <div id="mithrilapp"> Mithril: <span id="mithrilMountNode"></span> </div> <!-- X --> <div id="xapp"> X: <span id="xmount"></span> </div> <script> var Ctrl = function($scope){ $scope.data = []; }; var KOData = ko.observableArray(); var KOviewmodel = {data: KOData}; var ENV = {EXTEND_PROTOTYPES: false}; var ReactComponent = React.createClass({displayName: 'PerfTest', getInitialState: function() { return { data: [] }; }, render: function() { var res = []; for(var i = 0; i < this.state.data.length; i++) { res.push(React.DOM.span({ key: 'test' + i }, this.state.data[i])); } return React.DOM.span(null, res); } }); var MithrilData = new Array(); var mithapp = { controller: function() { this.data = MithrilData; }, view: function(ctrl) { return m("span", [ctrl.data.map(function(datum) { return m('span', datum); })]); } } $(document).ready(function() { angular.element(document).ready(function() { var ang_scope = $('#angList').scope(); window.ANGclear = function(){ ang_scope.data.splice(0, ang_scope.data.length); ang_scope.$digest(); }; window.ANGpush = function(data){ ang_scope.data.push(data + ang_scope.data.length); ang_scope.$digest(); }; }); ko.applyBindings(KOviewmodel, document.getElementById('koapp')); window.KOclear = function (){ KOData.splice(0, KOData().length); }; window.KOpush = function (data){ KOData.push(data); }; EMapp = Ember.Application.create({ rootElement: '#emapp' }); EMapp.data = Ember.A(); window.EMclear = function () { EMapp.data.clear(); }; window.EMpush = function (data) { EMapp.data.pushObject(data); }; var reactComp = ReactComponent(); React.renderComponent(reactComp, $('#reactMountNode')[0]); window.RClear = function() { reactComp.setState({data: []}) }; window.RPush = function(data) { var stateData = reactComp.state.data; stateData.push(data); reactComp.setState({data: stateData}); } m.module(document.getElementById("mithrilMountNode"), mithapp); window.Mclear = function() { m.startComputation(); MithrilData.splice(0); m.endComputation(); }; window.Mpush = function(data) { m.startComputation(); MithrilData.push(data); m.endComputation(); } }); </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
Ember 100
EMclear();
for (var i = 0; i < 100; i++)
  EMpush("emitem");
pending…
X 100
while(el("xmount").firstChild){
  el("xmount").removeChild(el("xmount").firstChild);
}

var x = [];
for (var i=0;i<100;i++) x[i] = t("span", {})("xitem");

el("xmount").appendChild(t()(x));
pending…
Mithril 100
Mclear();
for (var i = 0; i < 100; i++)
  Mpush("mitem");
pending…
React 100
RClear();
for (var i = 0; i < 100; i++)
  RPush("ritem");
pending…
Angular 100
ANGclear();
for (var i = 0; i < 100; i++)
  ANGpush("ngitem");
pending…
Knockout 100
KOclear();
for (var i = 0; i < 100; i++)
  KOpush("koitem");
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.

0 Comments

Angular: {{item}}
Knockout:
React:
Mithril:
X: