const el = document.createElement('div');
el.innerHTML = `
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<button>Button</button>
`;
document.documentElement.appendChild(el);
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
document.documentElement.appendChild(el);
const fg = document.createDocumentFragment();
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
fg.appendChild(heading);
fg.appendChild(para1);
fg.appendChild(para2);
fg.appendChild(button);
el.appendChild(fg);
document.documentElement.appendChild(el);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
appendChild | |
documentFragment |
Test name | Executions per second |
---|---|
innerHTML | 65425.1 Ops/sec |
appendChild | 65538.5 Ops/sec |
documentFragment | 39263.8 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
What is being tested?
The benchmark is comparing three ways to append content to an HTML element:
innerHTML
appendChild
(appending a single element)documentFragment
These methods are used to add content to an HTML element, such as a <div>
or another <html>
element.
Options compared
The benchmark compares the performance of each method on three different browsers:
Each browser's performance is measured in executions per second (ExecutionsPerSecond).
Pros and Cons of each approach:
innerHTML
:appendChild
(appending a single element):innerHTML
, as it doesn't involve DOM parsing. However, it requires creating new elements and appending them individually, which can lead to more DOM nodes being created.documentFragment
:innerHTML
, as it allows the browser to batch append elements together before updating the DOM. This can reduce the number of DOM updates, which can improve performance.Library and its purpose
In this benchmark, no specific JavaScript library is used beyond document
and HTMLElement
. However, documentFragment
relies on the browser's built-in support for fragmenting HTML elements.
Special JS feature or syntax
There are no special JS features or syntax used in this benchmark. The test cases only use standard JavaScript features like createElement
, appendChild
, and innerHTML
.
Other alternatives
If you're looking to optimize DOM manipulation, consider the following alternatives:
insertBefore
instead of appendChild
.Keep in mind that the best approach will depend on your specific use case, application requirements, and performance constraints.