<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 = '';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
removeChild | |
removeChild backwards | |
innerText |
Test name | Executions per second |
---|---|
innerHTML | 4110898.5 Ops/sec |
removeChild | 4960684.5 Ops/sec |
removeChild backwards | 4917647.5 Ops/sec |
innerText | 3756438.0 Ops/sec |
Let's break down the benchmark and analyze each test case.
Benchmark Definition Json
The provided JSON defines two benchmarks: a script preparation code and four individual test cases. The script preparation code is used to create a HTML container element with 1000 child elements, which will be used as a baseline for the subsequent test cases.
Script Preparation Code
var node = document.getElementById('container');
for (var i = 0; i < 1000; i++) node.appendChild(document.createElement('div'));
This code creates an HTML container element with the ID "container" and appends 1000 new child elements to it using a for
loop.
Individual Test Cases
The four test cases are:
removeChild()
method.removeChild()
method with a reference to the last child element.Library and Syntax
None of these test cases use any JavaScript libraries or special features. The syntax used is standard JavaScript DOM manipulation methods.
Options Compared
The benchmark compares four different approaches:
innerHTML
: Setting the inner HTML of the element.removeChild (firstChild)
: Removing the first child element using removeChild()
with a reference to the first child element.removeChild (lastChild)
: Removing the last child element using removeChild()
with a reference to the last child element.innerText
: Setting the inner text of the element.Pros and Cons
Here are some pros and cons for each approach:
removeChild (firstChild)
due to the additional reference calculation.innerHTML
due to the need to parse and manipulate the text content.Other Considerations
The benchmark may also consider other factors, such as:
Overall, the benchmark aims to compare the performance of these four approaches in a controlled environment, providing insights into their strengths and weaknesses.