customElements.define('x-innerhtml', class extends HTMLElement {
connectedCallback() {
this.innerHTML = '<span>.</span> ';
}
});
customElements.define('x-appendraw', class extends HTMLElement {
connectedCallback() {
const span = document.createElement('span');
span.textContent = '. ';
this.append(span);
}
});
customElements.define('x-appendchildraw', class extends HTMLElement {
connectedCallback() {
const span = document.createElement('span');
span.textContent = '. ';
this.appendChild(span);
}
});
const createElement = (tag, props = {}) => Object.assign(document.createElement(tag), props);
customElements.define('x-prepend', class extends HTMLElement {
connectedCallback() {
this.prepend(createElement("span", {
textContent: ". "
}));
}
});
customElements.define('x-append', class extends HTMLElement {
connectedCallback() {
this.append(createElement("span", {
textContent: ". "
}));
}
});
document.createElement("x-innerhtml");
document.createElement("x-appendraw");
document.createElement("x-appendchildraw");
document.createElement("x-prepend");
document.createElement("x-append");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
x-innerHTML | |
x-appendraw | |
x-appenchilddraw | |
x-prepend | |
x-append |
Test name | Executions per second |
---|---|
x-innerHTML | 419903.2 Ops/sec |
x-appendraw | 422957.7 Ops/sec |
x-appenchilddraw | 384945.8 Ops/sec |
x-prepend | 356556.5 Ops/sec |
x-append | 321177.8 Ops/sec |
Let's break down the benchmark and its various components.
Benchmark Overview
The benchmark, named "Web Component append content 4 ways", tests the performance of different methods for appending content to a Web Component (WC). A WC is an HTML element that can be customized with custom properties. In this case, we're creating four types of WCs:
x-innerHTML
x-appendraw
x-appendchildraw
x-prepend
x-append
Test Case Breakdown
Each test case measures the performance of a specific WC method.
x-innerHTML
: Tests appending content using the innerHTML
property.x-appendraw
: Tests appending content using the append()
method.x-appendchildraw
: Tests appending content using the appendChild()
method.x-prepend
: Tests prepping content before appending it using the prepend()
method.x-append
: Tests appending content using a custom function, createElement()
, and passing it to the WC.Library Usage
The benchmark uses customElements.define()
to create the WCs. This library is part of the Web Components specification, which provides a way to define custom HTML elements.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark that require specific handling.
Other Alternatives
To compare the performance of these methods, alternative approaches could be explored, such as:
However, these alternatives are not part of the current benchmark and would require significant changes to the test suite.
Keep in mind that this is just a general overview, and specific results may vary depending on the browser, hardware, and other factors.