<div id="container"></div>
var node = document.getElementById('container');
for(var i = 0; i < 100; i++) node.appendChild(document.createElement('div'));
var node = document.getElementById('container');
node.innerHTML = '';
var node = document.getElementById('container');
var child;
while(child=node.firstChild) node.removeChild(child)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
removeChild |
Test name | Executions per second |
---|---|
innerHTML | 1950645.2 Ops/sec |
removeChild | 2448661.8 Ops/sec |
Let's break down the provided JSON data and explain what is being tested, compared, and the pros/cons of each approach.
Benchmark Overview
The benchmark compares two approaches to clear an HTML node:
innerHTML
: setting the innerHTML property directly on the element.removeChild
: repeatedly removing child nodes using removeChild()
method.Script Preparation Code
The script preparation code creates a container HTML element and appends 100 child elements to it:
var node = document.getElementById('container');
for (var i = 0; i < 100; i++) {
node.appendChild(document.createElement('div'));
}
This code sets up the test scenario by creating a large number of child elements, which will be used to measure the performance of each approach.
Html Preparation Code
The HTML preparation code defines an empty container element with an ID of "container":
<div id="container"></div>
This code provides the context for the script preparation code and allows the test to run on a real DOM node.
Individual Test Cases
There are two individual test cases:
innerHTML
:var node = document.getElementById('container');
node.innerHTML = '';
This test case measures the performance of setting the innerHTML property directly on the element.
removeChild
:var node = document.getElementById('container');
var child;
while (child = node.firstChild) {
node.removeChild(child);
}
This test case repeatedly removes child nodes using a while loop and the removeChild()
method.
Library Used
There is no explicit library mentioned in the JSON data. However, both tests use the document
object, which is part of the DOM (Document Object Model) API provided by web browsers.
Special JS Features or Syntax
Neither test case uses any special JavaScript features or syntax. The tests are designed to be simple and straightforward, focusing on the performance differences between two approaches.
Pros and Cons of Each Approach
innerHTML
:removeChild
:Other Alternatives
If you need to clear an HTML node, other alternatives might include:
innerText
property (not supported in older browsers).empty()
methoddocument.createElement('div')
.clear()`Keep in mind that these alternatives may have different performance characteristics and use cases, so it's essential to test and evaluate them based on your specific requirements.
In summary, this benchmark compares two simple approaches to clearing an HTML node, highlighting the trade-offs between setting innerHTML and removing child nodes using removeChild()
method.