Editing Knockout.js vs Backbone.js vs Direct DOM Manipulation This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <div id="test"> <ul id="knockout" data-bind="foreach: jokes"> <li data-bind="text: joke"/> </ul> <ul id="dom"> </ul> <ul id="backbone"> </ul> </div> <script type="text/template" id="backbone-template"> <li><%= joke %></li> </script> <script src="//cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.1/backbone-min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) // Reset DOM var el = document.getElementById('test'); el.outerHTML = '<div id="test"><ul id="knockout" data-bind="foreach: jokes"><li data-bind="text: joke"/></ul><ul id="dom"></ul><ul id="backbone"></ul></div>'; var jokes = [{joke:"How many programmers does it take to change a light bulb? None – It’s a hardware problem"}, {joke:"Programming is like sex: One mistake and you have to support it for the rest of your life."}, {joke:"There are three kinds of lies: Dark Lies, white lies, and benchmarks."}]; /* Knockout.js */ function ViewModel() { this.jokes = jokes; } /* Backbone.js */ Backbone.setDomLibrary(jQuery); var Model = Backbone.Model.extend({ defaults: function() { return { joke: null }; } }); var Collection = Backbone.Collection.extend({ model:Model }); var collection = new Collection(); var View = Backbone.View.extend({ tagName:'li', template: _.template($('#backbone-template').html()), render: function() { $(this.el).html(this.template(this.model.toJSON())); return this; } }); var AppView = Backbone.View.extend({ el: $('#backbone'), initialize: function() { collection.bind('add', this.addOne, this); collection.bind('reset', this.addAll, this); collection.reset(jokes); }, addOne: function(joke) { var view = new View({model: joke}); $('#backbone').append(view.render().el); }, addAll: function() { collection.each(this.addOne); }, }); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code // Reset DOM var el = document.getElementById('test'); el.outerHTML = '<div id="test"><ul id="knockout" data-bind="foreach: jokes"><li data-bind="text: joke"/></ul><ul id="dom"></ul><ul id="backbone"></ul></div>'; el = document.getElementById('knockout'); // Run Knockout.js ko.applyBindings(new ViewModel(), document.getElementById('knockout')); Test 2 Title Async (check if this is an asynchronous test) Code // Reset DOM var el = document.getElementById('test'); el.outerHTML = '<div id="test"><ul id="knockout" data-bind="foreach: jokes"><li data-bind="text: joke"/></ul><ul id="dom"></ul><ul id="backbone"></ul></div>'; el = document.getElementById('dom'); // Manipulate DOM for(var i in jokes) { var li = document.createElement('li'); li.appendChild(document.createTextNode(jokes[i].joke)); el.appendChild(li); } Test 3 Title Async (check if this is an asynchronous test) Code // Reset DOM var el = document.getElementById('test'); el.outerHTML = '<div id="test"><ul id="knockout" data-bind="foreach: jokes"><li data-bind="text: joke"/></ul><ul id="dom"></ul><ul id="backbone"></ul></div>'; // Run Backbone.js collection = new Collection(); // workaround: collection.reset doesn't seem to clear old items var v = new AppView();