<div id="container"></div>
var node = document.getElementById('container');
for(var i = 0; i < 10000; i++) node.appendChild(document.createElement('div'));
var node = document.getElementById('container');
while(node.firstChild) node.removeChild(node.firstChild)
var node = document.getElementById('container');
while(node.firstChild) node.removeChild(node.lastChild)
var node = document.getElementById('container');
while(node.firstChild) node.firstChild.remove();
var node = document.getElementById('container');
while(node.firstChild) node.lastChild.remove();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
removeChild | |
removeChild (backwards) | |
children.remove | |
children.remove (backwards) |
Test name | Executions per second |
---|---|
removeChild | 5427728.0 Ops/sec |
removeChild (backwards) | 5447095.5 Ops/sec |
children.remove | 5317903.0 Ops/sec |
children.remove (backwards) | 5166087.0 Ops/sec |
Let's break down the provided benchmark and its components.
Benchmark Definition
The benchmark is designed to compare the performance of three different approaches for removing child elements from a DOM node:
removeChild
removeChild (backwards)
children.remove
Script Preparation Code
The script preparation code creates a container element with an ID of "container" and appends 10,000 child elements to it using appendChild
. This simulates a scenario where you need to remove multiple child elements.
Html Preparation Code
The HTML preparation code only includes the container element:
<div id="container"></div>
Individual Test Cases
Each test case represents one of the three approaches for removing child elements. Here's a brief explanation of each approach:
removeChild
:removeChild
method on the current node to remove its first child element.removeChild (backwards)
:removeChild
method on the current node, but with a twist: it starts by removing the last child element and works its way backwards.children.remove
:children
property on the Node object).remove
method on the children
array to remove all child elements at once.children.remove (backwards)
:Library Used
In the individual test cases, two libraries/libraries-like-approaches are used:
removeChild
and removeChild (backwards)
: These approaches rely on the built-in DOM methods for navigating the node's children.children.remove
and children.remove (backwards)
: These approaches use a modern browser-specific property (children
) to access the child elements.Special JS Feature or Syntax
The benchmark uses two modern features:
Arrow functions
in the script preparation code: The line while(node.firstChild) node.removeChild(node.firstChild);
uses an arrow function for simplicity.Modern browser-specific properties
: The use of children
property on the Node object is specific to modern browsers.Other Alternatives
In theory, there are other approaches to removing child elements from a DOM node:
Array.prototype.slice()
or Array.prototype.splice()
Node.iterateChildNodes()
and then iteratively removing each child elementHowever, these alternatives may not provide the same level of performance or efficiency as the approaches tested in this benchmark.
Pros and Cons of Different Approaches
Here's a brief summary of the pros and cons of each approach:
removeChild
:removeChild (backwards)
:children.remove
:children.remove (backwards)
:children.remove
, but with an additional twist.children.remove
due to the specific ordering.Benchmark Insights
From the provided benchmark results, we can see that:
children.remove
) consistently outperforms the other approaches.removeChild (backwards)
) is slower than the standard approach but still relatively efficient.removeChild
and removeChild (backwards)
approaches are closer in performance, with slight variations between browsers.These insights can inform decisions when choosing an approach for removing child elements from a DOM node, depending on the specific requirements of your use case.