Editing Element Creation 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) Compare various methods of creating DOM elements. 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="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <style> .hidden { display: none; } </style> <div class="hidden" id="container"> </div> 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) var container = document.getElementById('container'), dummy = document.createElement( 'div' ), d = document.createElement( 'div' ), templates = {}; function tag_ctor(tag, deep) { var template = document.createElement(tag); templates[tag] = template; window[tag] = function(attrs) { // var el = templates[tag].cloneNode(false); var el = document.createElement( tag ); el.attr = function(attrs) { Object.keys(attrs).forEach(function(it) { el.setAttribute(it, attrs[it]); }); return el; }; el.set = function() { el.innerHTML = ''; el.add.apply(window, arguments); } el.add = function() { for (var i = 0; i < arguments.length; i++) { if (arguments[i].nodeType === 1 || arguments[i].nodeType === 11) { el.appendChild(arguments[i]); } else { el.appendChild(document.createTextNode(arguments[i])); } } return el; }; return el; } } window.text = function(content) { return document.createTextNode(content); }; ['p', 'div', 'span', 'strong', 'em', 'img', 'table', 'tr', 'td', 'th', 'thead', 'tbody', 'tfoot', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'form', 'input', 'textarea', 'legend', 'fieldset', 'select', 'option', 'blockquote', 'cite', 'br', 'hr', 'dd', 'dl', 'dt', 'address', 'a', 'button', 'abbr', 'acronym', 'script', 'link', 'style', 'bdo', 'ins', 'del', 'object', 'param', 'col', 'colgroup', 'optgroup', 'caption', 'label', 'dfn', 'kbd', 'samp', 'var', 'header', 'section', 'aside', 'details', 'nav'].forEach(function(tag) { tag_ctor(tag); }); function collect(root) { var collection = {}, stack = [], n; if (root.nodeType !== 1) { return collection; } stack.push(root); while (stack.length !== 0) { n = stack.pop(); if (n.hasAttribute('id')) { collection[n.getAttribute('id')] = n; } var i = n.childNodes.length; while (i) { i--; if (n.childNodes[i].nodeType === 1) { stack.push(n.childNodes[i]); } } } return collection; } var alarm_text = '<div style="margin-bottom: 25px;"> \ <div style="width: 840px; margin: 10px;"> \ <div id="icon" class="ilb alarm" style="white-space: nowrap; padding-top: 10px;> \ <div id="message" class="ilb alarm" style="width: 380px;"></div> \ <div class="ilb alarm" style="width: 40px; color: #cccccc; font-size: 20px; padding-top: 10px;">@</div> \ <div id="locations" class="ilb alarm" style="width: 210px; color: #999999;"></div> \ <div id="time" class="ilb alarm" style="width: 100px; color: #999999;"> </div> \ </div> \ <details id="nested" style="display: none; margin: 10px;"></details> \ </div>', alarm_template = div().attr({ style: 'margin-bottom: 25px;' }).add( div().attr({ style: 'width: 840px; margin: 10px;' }).add( div().attr({ id: 'icon', class: 'ilb alarm', style: 'white-space: nowrap; padding-top: 10px;' }), div().attr({ id: 'message', class: 'ilb alarm', style: 'width: 380px;' }), div().attr({ class: 'ilb alarm', style: 'width: 40px; color: #cccccc; font-size: 20px; padding-top: 10px;' }).add( '@' ), div().attr({ id: 'locations', class: 'ilb alarm', style: 'width: 210px; color: #999999;' }), div().attr({ id: 'time', class: 'ilb alarm', style: 'width: 100px; color: #999999;' }) ), details().attr({ id: 'nested', style: 'display: none; margin: 10px;' }) ); dummy.innerHTML = alarm_text; var alarm_template_text = dummy.childNodes[ 0 ]; 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 container.innerHTML = alarm_text; var jq = $(container); jq.find('icon'); jq.find('message'); jq.find('locations'); jq.find('time'); jq.find('nested'); Test 2 Title Async (check if this is an asynchronous test) Code var a,b,c,d,e; container.appendChild( div().attr({ style: 'margin-bottom: 25px;' }).add( div().attr({ style: 'width: 840px; margin: 10px;' }).add( a = div().attr({ id: 'icon', class: 'ilb alarm', style: 'white-space: nowrap; padding-top: 10px;' }), b = div().attr({ id: 'message', class: 'ilb alarm', style: 'width: 380px;' }), div().attr({ class: 'ilb alarm', style: 'width: 40px; color: #cccccc; font-size: 20px; padding-top: 10px;' }).add( '@' ), c = div().attr({ id: 'locations', class: 'ilb alarm', style: 'width: 210px; color: #999999;' }), d = div().attr({ id: 'time', class: 'ilb alarm', style: 'width: 100px; color: #999999;' }) ), e = details().attr({ id: 'nested', style: 'display: none; margin: 10px;' }) ) ); Test 3 Title Async (check if this is an asynchronous test) Code container.appendChild( alarm_template.cloneNode( true )); var els = collect( container ); //d.cloneNode( true ); Test 4 Title Async (check if this is an asynchronous test) Code container.innerHTML = alarm_text; var els = collect( container ); //document.createElement( 'div' );