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);
// fragment.appendChild(el);
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');
const headingText = document.createTextNode('Heading');
const para1Text = document.createTextNode('Paragraph 1');
const para2Text = document.createTextNode('Paragraph 2');
const buttonText = document.createTextNode('Button');
heading.appendChild(headingText);
para1.appendChild(para1Text);
para2.appendChild(para2Text);
button.appendChild(buttonText);
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
document.documentElement.appendChild(el);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
createElement createTextNode |
Test name | Executions per second |
---|---|
createElement | 93910.6 Ops/sec |
createElement createTextNode | 56520.5 Ops/sec |
Measuring JavaScript performance is an essential task in web development, and tools like MeasureThat.net help us understand how different browsers and platforms handle similar tasks.
Let's break down the provided benchmark:
Benchmark Definition
The CreateElement
benchmark measures the time it takes to create multiple elements (a div
, two p
elements, a button
) and append them to another element using the appendChild
method. This is a basic DOM manipulation task that can be optimized in different ways.
There are two variations of this benchmark:
createElement
: This test creates all elements as child nodes of another element (el
). This approach has some overhead due to the creation and appending of multiple nodes.createElement createTextNode
: In this version, the text content is created using document.createTextNode()
instead of assigning a string value directly to an element's innerText
property or using HTML attributes. This approach can be more efficient, especially for larger text contents.Options Compared
The two variations compare:
appendChild
) vs. creating them in a single operation ( possibly optimized by the browser).innerText
properties to set text content vs. creating separate textNode
objects and appending them to elements.Pros and Cons
createElement
:createElement createTextNode
:textNode
objects).Other Considerations
When optimizing this benchmark, consider factors like:
Library Usage
None of the benchmarks explicitly use any JavaScript libraries. However, some browsers may have built-in optimizations or features that affect the performance of this benchmark, such as:
requestAnimationFrame
optimization for DOM manipulation.Special JS Features/Syntax
None of the benchmarks explicitly use special JavaScript features like async/await, generators, or classes. However, some modern browsers may optimize or handle these features differently.
In summary, MeasureThat.net provides a simple and convenient way to measure JavaScript performance, allowing developers to compare different approaches to optimizing DOM manipulation tasks.