<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
Object.defineProperty(HTMLElement, 'From', {
enumerable: false,
value: (function (doc, rgx) {
//https://www.measurethat.net/Benchmarks/Show/2149/0/element-creation-speed
return function CreateElementFromHTML(html) {
html = html.trim();
var bodystart = html.indexOf('>') + 1, bodyend = html.lastIndexOf('<');
var elemStart = html.substr(0, bodystart);
var innerHTML = html.substr(bodystart, bodyend - bodystart);
rgx.lastIndex = 0;
var elem = doc.createElement(rgx.exec(elemStart)[4]);
var match; while ((match = rgx.exec(elemStart))) {
if (match[1] === undefined) {
elem.setAttribute(match[4], "");
} else {
elem.setAttribute(match[1], match[3]);
}
}
elem.innerHTML = innerHTML;
return elem;
};
}(window.document, /(\S+)=(["'])(.*?)(?:\2)|(\w+)/g))
});
var domParser = new DOMParser();
var arr = Array(2);
var el = document.createElement('link');
el.id='reddit_css';
el.type='text/css';
el.rel='stylesheet';
el.setAttribute('data-srcprop', 'href');
el.setAttribute('data-src', 'sites/reddit/zinject.reddit.css');
el.async = true;
el.defer = true;
arr[0] = el;
el = document.createElement('div');
el.id = 'somevalue';
el.className = 'sidebar';
el.innerHTML = `<div class='sb-handle'></div><div class='sb-track'></div>`;
arr[1] = el;
return arr;
var arr = Array(2);
arr[0] = $(`<link id="reddit_css" type="text/css" rel="stylesheet" async defer data-srcprop="href" data-src="sites/reddit/zinject.reddit.css">`);
arr[1] = $(`<div id='somevalue' class='sidebar'><div class='sb-handle'></div><div class='sb-track'></div></div>`);
return arr;
var arr = Array(2);
arr[0] = HTMLElement.From(`<link id="reddit_css" type="text/css" rel="stylesheet" async defer data-srcprop="href" data-src="sites/reddit/zinject.reddit.css">`);
arr[1] = HTMLElement.From(`<div id='somevalue' class='sidebar'><div class='sb-handle'></div><div class='sb-track'></div></div>`);
return arr;
var arr = Array(2);
var doc = domParser.parseFromString(`<link id="reddit_css" type="text/css" rel="stylesheet" async defer data-srcprop="href" data-src="sites/reddit/zinject.reddit.css">`, 'text/html');
arr[0] = doc.head.removeChild(doc.head.firstElementChild);
doc = domParser.parseFromString(`<div id='somevalue' class='sidebar'><div class='sb-handle'></div><div class='sb-track'></div></div>`, 'text/html');
arr[1] = doc.body.removeChild(doc.body.firstElementChild);
return arr;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Programmatic Element creation | |
jQuery Element creation | |
Custom Method: HTMLElement.From() | |
DOMParser |
Test name | Executions per second |
---|---|
Programmatic Element creation | 81233.5 Ops/sec |
jQuery Element creation | 17719.5 Ops/sec |
Custom Method: HTMLElement.From() | 8580.8 Ops/sec |
DOMParser | 4542.7 Ops/sec |
I'll provide an explanation of the benchmark, its options, pros and cons, and other considerations.
Benchmark Overview
The benchmark measures the speed of different methods to create HTML elements in JavaScript. The test cases involve creating two types of elements: a link and a div. Each test case uses a different approach to create these elements:
document.createElement()
and sets its attributes.HTMLElement.From()
to create the elements, which is defined in the benchmark preparation code.DOMParser
and extracts the first child element.Options Compared
The benchmark compares four different approaches:
document.createElement()
to create elements.DOMParser
and extracting the first child element.Pros and Cons of Each Approach
Other Considerations
The benchmark provides a useful comparison of different methods for creating HTML elements in JavaScript, highlighting the pros and cons of each approach.