Editing DOM vs innerHTML based Templating 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) A brief comparison of some JavaScript templating engines on a short template: 7 DOM nodes ... 7 interpolated values. 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) <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="https://raw.github.com/pure/pure/master/libs/pure.js"></script> <script src="http://documentcloud.github.com/underscore/underscore.js"></script> <script src="https://raw.github.com/janl/mustache.js/master/mustache.js"></script> <script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"> </script> <script type="text/javascript" src="https://raw.github.com/jquery/sizzle/master/sizzle.js"> </script> <script type="text/javascript" src="https://github.com/downloads/kononencheg/Tuna-Common/tuna-common.js"> </script> <script type="text/javascript" src="https://github.com/downloads/kononencheg/Tuna-Templates/tuna-templates.js"> </script> <div id="mustache"></div> <div class="purejs_template"> <div> <h1 class="header"></h1> <div class='a'></div> <div class='b'></div> <div class='c'></div> <div class='d'></div> <div class='e'></div> <div class='f'></div> </div> </div> <div id="underscore"></div> <div id="tuna"> <div> <h1 class="t-header"></h1> <div class='t-a'></div> <div class='t-b'></div> <div class='t-c'></div> <div class='t-d'></div> <div class='t-e'></div> <div class='t-f'></div> </div> </div> <div xmlns:tuna style="display: none;"> <tuna:template id="template"> <tuna:spot tuna:target="t-header" tuna:path="header" /> <tuna:spot tuna:target="t-a" tuna:path="a" /> <tuna:spot tuna:target="t-b" tuna:path="b" /> <tuna:spot tuna:target="t-c" tuna:path="c" /> <tuna:spot tuna:target="t-d" tuna:path="d" /> <tuna:spot tuna:target="t-e" tuna:path="e" /> <tuna:spot tuna:target="t-f" tuna:path="f" /> </tuna:template> </div> <script> window.mustacheTemplate = "<div><h1 class='header'>{{header}}</h1><div class='a'>{{a}}</div><div class='b'>{{b}}</div><div class='c'>{{c}}</div><div class='d'>{{d}}</div><div class='e'>{{e}}</div><div class='f'>{{f}}</div></div>"; window.underscoreTemplate = _.template("<div><h1 class='header'><%=header%></h1><div class='a'><%=a%></div><div class='b'><%=b%></div><div class='c'><%=c%></div><div class='d'><%=d%></div><div class='e'><%=e%></div><div class='f'><%=f%></div></div>"); window.sharedVariables = { header: "Header", a: 'aaa', b: 'bbb', c: 'ccc', d: 'ddd', e: 'eee', f: 'fff' }; var mustacheContainer = document.getElementById("mustache"); var underscoreContainer = document.getElementById("underscore"); var tunaContainer = document.getElementById('tuna'); var template = tuna.tmpl.compileFromMarkup(tunaContainer, 'template'); </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) 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 mustacheContainer.innerHTML = Mustache.to_html(mustacheTemplate, sharedVariables); Test 2 Title Async (check if this is an asynchronous test) Code $('.purejs_template').autoRender(sharedVariables); Test 3 Title Async (check if this is an asynchronous test) Code underscoreContainer.innerHTML = underscoreTemplate(sharedVariables); Test 4 Title Async (check if this is an asynchronous test) Code template.processTransform(sharedVariables);