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 idx = html.indexOf('>') + 1; var elemStart = html.substr(0, idx); html = html.substr(idx).SubstrBeforeLast('<'); rgx.lastIndex = 0; var elem = document.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 = html; 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 complex benchmark! It looks like you want me to analyze the latest benchmark result, which is an array of objects containing information about browser and device characteristics. Here's my attempt to break it down: **Browser and Device Information** * Browser: Chrome 63 * Device Platform: Other ( likely a desktop or laptop) * Operating System: Windows **Benchmark Results** There are four benchmark results in the latest result set. Let me summarize each one: 1. **Procedural Element creation**: 50278 executions per second * This suggests that using procedural methods to create elements (e.g., `document.createElement`) is relatively fast. 2. **Custom Method: document.CreateElementFromHTML()**: 30775 executions per second * Similar to the first one, but with a custom method for creating elements. 3. **Custom Method: HTMLElement.From()`**: 29190 executions per second * Another custom method for creating elements. 4. **jQuery Element creation**: 14972 executions per second * Using jQuery's `$(...)` syntax to create elements is slower compared to the other methods. **Observations** * The first three benchmark results are relatively close, suggesting that these custom methods (Procedural and Custom Methods) have similar performance characteristics. * The jQuery Element creation result is significantly lower than the others, indicating that using jQuery might introduce additional overhead or complexities in this specific test case. Please let me know if you'd like me to elaborate on any of these points or provide further insights!
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?