<div><div><div></div></div></div>
<div></div>
<a href='#' />
<div></div>
<div></div>
<div><div><div></div></div></div>
<div></div>
<div><div></div></div>
<div><div></div></div>
<div><div><div><div></div></div></div></div>
<div></div>
<div></div>
for(let i of [Document,DocumentFragment,Element]){
i.prototype.$=i.prototype.querySelector;
i.prototype.$$=function(s){return[this.querySelectorAll(s)]};
}
class UI {
constructor(doc){
this.#dlg = doc.body.appendChild(doc.createElement('dialog'));
this.#dlg.id = 'dlg';
this.#dlg.className = 'fit';
this.#img = this.#dlg.appendChild(new Image());
}
#dlg;
#img;
}
const ui = new UI(document);
class UI {
constructor(doc){
this.#dlg = doc.body.appendChild(doc.createElement('dialog'));
this.#dlg.id = 'dlg';
this.#dlg.className = 'fit';
this.#img = this.#dlg.appendChild(document.createElement('img'));
}
#dlg;
#img;
}
const ui = new UI(document);
class UI {
constructor(doc){
this.#dlg = doc.body.appendChild(Object.assign(doc.createElement('dialog'),{id:'dlg',className:'fit'}));
this.#img = this.#dlg.appendChild(new Image());
}
#dlg;
#img;
}
const ui = new UI(document);
class UI {
constructor(doc){
doc.body.insertAdjacentHTML('beforeend',`<dialog id="dlg" class="fit"><img></dialog>`);
this.#dlg = doc.body.$('#dlg');
this.#img = this.#dlg.$('img');
}
#dlg;
#img;
}
const ui = new UI(document);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
appendChild | |
appendChild 2 | |
appendChild 3 | |
insertAdjacentHTML |
Test name | Executions per second |
---|---|
appendChild | 4257.5 Ops/sec |
appendChild 2 | 1876.1 Ops/sec |
appendChild 3 | 1733.5 Ops/sec |
insertAdjacentHTML | 1497.9 Ops/sec |
To explain what is being tested on the provided JSON, let's break down each test case.
Test Case 1: appendChild
This test case creates a new dialog element using the appendChild
method and then appends an image to it. The dialogue is assigned an ID and a class name.
The goal of this test case is to measure how long it takes for Firefox to append an image to a newly created dialog element.
Test Case 2: appendChild 2
This test case is similar to the first one, but instead of using the createElement
method to create the dialogue, it uses Object.assign
to merge properties into the existing dialog
element. This approach can be faster for larger dialogues or when the template needs to be merged with data.
The goal of this test case is still to measure how long it takes for Firefox to append an image to a newly created dialog element, but using a different approach.
Test Case 3: appendChild 3
This test case uses the same Object.assign
method as Test Case 2, but without creating the dialogue first. Instead, it directly appends HTML content to the document body using insertAdjacentHTML
. This approach can be faster because it avoids the overhead of creating an additional element.
The goal of this test case is to measure how long it takes for Firefox to append an image to a new HTML content fragment inserted into the document body.
Test Case 4: insertAdjacentHTML
This test case uses the insertAdjacentHTML
method to insert a dialog element and its content into the document body. The dialogue is assigned an ID and a class name, just like in Test Cases 1-3.
The goal of this test case is to measure how long it takes for Firefox to render a pre-existing HTML fragment inserted into the document body using insertAdjacentHTML
.
Comparison of Approaches
appendChild
: This approach involves creating an element and then appending content to it. It can be slower because it requires extra DOM operations.Object.assign
: This approach can be faster than appendChild
for larger dialogues or when merging data with the template. However, it may not be suitable if the template needs to be created from scratch.insertAdjacentHTML
: This approach is often the fastest because it avoids creating an additional element and directly inserts HTML content into the document body.Pros and Cons
appendChild
:Object.assign
:appendChild
, suitable for larger dialogues or data merginginsertAdjacentHTML
:Other Considerations
In terms of alternatives, some other methods for appending content or creating dialogues could be explored, such as:
However, without more information on the specific requirements and constraints of the project, it's difficult to suggest alternative approaches that would be equally effective.