<div id="container"></div>
var node = document.getElementById('container');
for(var i = 0; i < 1000; i++) node.appendChild(document.createElement('div'));
var node = document.getElementById('container');
node.innerHTML = '';
var node = document.getElementById('container');
while(node.firstChild) node.removeChild(node.firstChild)
var node = document.getElementById('container');
while(node.lastChild) node.removeChild(node.lastChild)
var node = document.getElementById('container');
node.innerText = '';
var node = document.getElementById('container');
node.textContent = '';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
removeChild | |
removeChild backwards | |
innerText | |
textContent |
Test name | Executions per second |
---|---|
innerHTML | 2929692.0 Ops/sec |
removeChild | 3823414.0 Ops/sec |
removeChild backwards | 3654463.2 Ops/sec |
innerText | 2284382.2 Ops/sec |
textContent | 2920597.8 Ops/sec |
Let's break down the provided JSON and explain what is being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark definition represents five different methods to clear the inner content of an HTML element:
innerHTML
: Setting the innerHTML
property directly on the DOM node.removeChild(firstChild)
: Removing the first child node of the current node using removeChild()
method and then removing all subsequent children.removeChild(lastChild)
: Removing the last child node of the current node using removeChild()
method and then removing all subsequent children in reverse order (backwards).innerText
: Setting the innerText
property directly on the DOM node.textContent
: Setting the textContent
property directly on the DOM node.Benchmark Preparation Code
The script preparation code creates a container element with 1000 child elements using appendChild()
method. This is done to simulate a scenario where the inner content needs to be cleared frequently.
Html Preparation Code
The HTML preparation code simply includes an empty container element (<div id="container"></div>
).
Individual Test Cases
Each test case represents one of the five methods mentioned above. The Benchmark Definition
property contains the JavaScript code for each method, and the Test Name
property provides a descriptive name for each test.
Let's analyze each test case:
innerHTML
: This is the most straightforward approach, as it directly sets the inner content to an empty string using the innerHTML
property.removeChild(firstChild)
: This method removes the first child node and then all subsequent children using a loop. While this approach can be more efficient than innerHTML
, it may not be suitable for all scenarios, especially those where the inner content needs to be accessed frequently.innerHTML
if the inner content is rarely accessed, as only one child node needs to be removed at a time.innerHTML
.removeChild(lastChild)
: Similar to the previous method, but in reverse order (backwards). This approach may not be suitable for all scenarios where the inner content needs to be cleared frequently, as it requires iterating over the children in reverse order.innerHTML
if the inner content is rarely accessed, as only one child node needs to be removed at a time (in reverse order).innerHTML
.innerText
: This method sets the innerText
property directly on the DOM node.innerHTML
, as it only requires accessing the inner text content without parsing HTML markup.innerText
property, which can affect compatibility.textContent
: This method sets the textContent
property directly on the DOM node.innerText
, as it only requires accessing the inner text content without parsing HTML markup.textContent
property, which can affect compatibility.Library
The provided benchmark code uses no libraries. It relies solely on standard JavaScript DOM API methods for manipulating elements and accessing their contents.
Special JS Feature/Syntax
None of the test cases use any special JavaScript features or syntax beyond what's required for standard DOM manipulation.