Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Element creation speed
(version: 0)
Comparing speed of creating elements
Comparing performance of:
Programmatic Element creation vs jQuery Element creation vs Custom Method: document.CreateElementFromHTML() vs Custom Method: HTMLElement.From()
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <div id="testElement"></div>
Script Preparation code:
function assignProps(proto, obj) { var descriptors = Object.getOwnPropertyDescriptors(obj); for (var desc in descriptors) { var prop = descriptors[desc]; prop.enumerable = false; Object.defineProperty(proto, desc, prop); } } (function StringPrototypeExtensions() { assignProps(String.prototype, { SubstrBefore: function substrBefore(sequence, bIncludeSequence) { var idx = this.indexOf(sequence); if (idx >= 0) { if (bIncludeSequence) { idx += sequence.length; } if (idx <= this.length) { return this.substr(0, idx); } } return this; }, SubstrAfter: function substrAfter(sequence, bIncludeSequence) { var idx = this.indexOf(sequence); if (idx >= 0) { if (!bIncludeSequence) { idx += sequence.length; } if (idx <= this.length) { return this.substr(idx); } } return this; }, SubstrBeforeLast: function substrBeforeLast(sequence, bIncludeSequence) { var idx = this.lastIndexOf(sequence); if (idx >= 0) { if (bIncludeSequence) { idx += sequence.length; } if (idx <= this.length) { return this.substr(0, idx); } } return this; }, SubstrAfterLast: function substrAfterLast(sequence, bIncludeSequence) { var idx = this.lastIndexOf(sequence); if (idx >= 0) { if (!bIncludeSequence) { idx += sequence.length; } if (idx <= this.length) { return this.substr(idx); } } return this; }, }); }()); (function RegexPrototypeExtensions() { assignProps(RegExp.prototype, { GetMatches: function GetMatches(str) { this.lastIndex = 0; var matches = [], match = null; while (match = this.exec(str)) { matches.push(match); } return matches; }, }); }()); (function DocumentExtensions() { assignProps(document, { CreateElementFromHTML: (function () { const rgxTag = /^\<(\w+)(?:\s+|\>)/; const rgxAttribs = /(\S+)=(["'])(.*?)(?:\2)/g; return function CreateElementFromHTML(html) { html = html.trim(); var innerhtml = html.SubstrAfter('>').SubstrBeforeLast('<'); var strelem = html.SubstrBefore('>', true); var tag = rgxTag.exec(strelem)[1]; var attribs = rgxAttribs.GetMatches(strelem); var elem = document.createElement(tag); for (var i = 0, len = attribs.length; i < len; i++) { var match = attribs[i]; elem.setAttribute(match[1], match[3]); } elem.innerHTML = innerhtml; return elem; }; }()), }); }()); (function ElementExtensions() { assignProps(HTMLElement, { From: (function (doc, rgx) { 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)) }); }());
Tests:
Programmatic Element creation
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;
jQuery Element creation
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;
Custom Method: document.CreateElementFromHTML()
var arr = Array(2); arr[0] = document.CreateElementFromHTML(`<link id="reddit_css" type="text/css" rel="stylesheet" async="" defer="" data-srcprop="href" data-src="sites/reddit/zinject.reddit.css">`); arr[1] = document.CreateElementFromHTML(`<div id='somevalue' class='sidebar'><div class='sb-handle'></div><div class='sb-track'></div></div>`); return arr;
Custom Method: HTMLElement.From()
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Programmatic Element creation
jQuery Element creation
Custom Method: document.CreateElementFromHTML()
Custom Method: HTMLElement.From()
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the provided benchmark results, it appears that the order of performance is: 1. **Custom Method: HTMLElement.From()** - with an average executions per second (EPS) of 11611.7919921875 2. **Programmatic Element creation** - with an EPS of 17596.94140625 3. **Custom Method: document.CreateElementFromHTML()** - with an EPS of 4414.7529296875 4. **jQuery Element creation** - with an EPS of 12266.4111328125 This suggests that the custom methods (HTMLElement.From and document.CreateElementFromHTML) are outperforming the jQuery method by a significant margin, indicating that these methods are more efficient in terms of performance. However, it's essential to note that this analysis is based on a specific set of benchmark results and may not be representative of all use cases or environments.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty vs. Object.prototype.hasOwnProperty.call
undefined vs. typeof vs. in vs. hasOwnProperty vs. Optional chaining
Polymorhpic: undefined vs. typeof vs. in vs. hasOwnProperty vs Object.hasOwn
typeof vs cached typeof (3 scans) (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?