<div id='target'></div>
const target = document.getElementById('target');
for(let i = 1; i <= 1000; i++) {
target.insertAdjacentHTML('beforeend', `<span>${i}</span>`);
}
const spans = new Array(1000).fill(0).map((v,i) => {return`<span>${i}</span>`}).join("");
document.getElementById('target').innerHTML = spans;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
insertAdjacentHTML | |
innerHTML |
Test name | Executions per second |
---|---|
insertAdjacentHTML | 440.9 Ops/sec |
innerHTML | 1250.2 Ops/sec |
Let's break down the provided JSON data and explain what is being tested, compared, and other considerations.
Benchmark Definition
The benchmark definition is a simple script that creates two JavaScript functions to insert HTML content into an element: insertAdjacentHtml
(loop) and innerHTML
(once). The purpose of this benchmark is to compare the performance of these two approaches in inserting HTML content.
Script Preparation Code
There is no script preparation code provided, which means that the test starts from a clean slate. This is good because it allows for a fair comparison between the two functions.
Html Preparation Code
The html preparation code creates a simple HTML element with an ID of "target". This element will be used as the target for inserting HTML content.
Individual Test Cases
There are two individual test cases:
insertAdjacentHtml
: This test case uses a loop to insert 1000 <span>
elements, one after another, using the insertAdjacentHTML
function.innerHTML
: This test case creates an array of 1000 <span>
elements and then uses the innerHTML
property to set the content of the target element.Library and Purpose
In both test cases, no external libraries are used. However, it's worth noting that the insertAdjacentHTML
function is supported by most modern browsers, while innerHTML
has been around since older versions of Internet Explorer.
Special JS Features or Syntax
There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. The focus is on comparing two basic functions for inserting HTML content.
Pros and Cons
Here are some pros and cons of each approach:
insertAdjacentHtml
(loop):innerHTML
:Other Considerations
When comparing these two approaches, it's essential to consider the following factors:
innerHTML
might be faster. For larger numbers (e.g., 1000+), insertAdjacentHtml
might be more efficient.innerHTML
might be sufficient. However, if the content is complex or contains multiple elements, insertAdjacentHtml
might be a better choice.Alternatives
If you're interested in exploring other alternatives, here are a few:
textContent
: This property can be used to set the text content of an element without creating a new HTML string.createElement
and appendChild
: These methods can be used to create elements dynamically and append them to another element.renderList
: Some libraries (e.g., React, Angular) use this method to render lists of elements.Keep in mind that each approach has its pros and cons, and the best choice depends on your specific use case and performance requirements.