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:
Procedural 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 () { const rgx = /(?:(\S+)=(["'])(.*?)(?:\2))|(\w+)/g; return function CreateElementFromHTML(html) { html = html.trim(); var elemStart = html.SubstrBefore('>', true); rgx.lastIndex = 0; var tag = rgx.exec(elemStart)[4]; var elem = document.createElement(tag); var match = null; while ((match = rgx.exec(elemStart))) { if (match[1] !== undefined) { elem.setAttribute(match[1], match[3]); } else { elem.setAttribute(match[4], ""); } } elem.innerHTML = html.SubstrAfter('>').SubstrBeforeLast('<'); return elem; }; }()), }); }());
Tests:
Procedural Element creation
var arr = Array(2); var el = document.createElement('link'); el.id='ZInject_CSS_zinject_regexr'; el.type='text/css'; el.rel='stylesheet'; el.dataset.srcprop='href'; el.dataset.src='standalone/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="zinject_reddit_css" type="text/css" rel="stylesheet" async="" defer="" data-srcprop="href" data-src="standalone/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="zinject_reddit_css" type="text/css" rel="stylesheet" async="" defer="" data-srcprop="href" data-src="standalone/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="zinject_reddit_css" type="text/css" rel="stylesheet" async defer data-srcprop="href" data-src="standalone/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
Procedural 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):
A benchmark report! It looks like we have a comparison of four different methods for creating HTML elements: 1. **Procedural Element creation**: Creating elements using the native `document.createElement` method. 2. **jQuery Element creation**: Using jQuery's `$()` function to create elements. 3. **Custom Method: document.CreateElementFromHTML()**: A custom method created by a developer, likely using some internal implementation of `createElement`. 4. **Custom Method: HTMLElement.From()**: Another custom method created by a developer. The benchmark reports the number of executions per second for each test on various browsers and devices. Based on these results, it appears that: * Procedural Element creation is the fastest method overall. * Custom Method: document.CreateElementFromHTML() is faster than jQuery Element creation. * Custom Method: HTMLElement.From() is slower than both procedural element creation and custom `CreateElementFromHTML()`. Keep in mind that this analysis assumes that the custom methods are implemented correctly and efficiently.
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?