<html>
<head>
</head>
<body>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
<div>Test</div>
</body>
</html>
while (document.body.lastChild) document.body.lastChild.remove()
while (document.body.lastChild) document.body.removeChild(document.body.lastChild)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Remove | |
removeChild |
Test name | Executions per second |
---|---|
Remove | 3407066.8 Ops/sec |
removeChild | 3411684.8 Ops/sec |
Let's break down the benchmark and explain what's being tested, compared, and other considerations.
Benchmark Overview
The benchmark is designed to compare two different approaches for removing elements from an HTML document: remove()
and removeChild()
. The test case consists of a script that creates multiple identical HTML elements (<div>Test</div>
) in the body of an HTML document. Then, it repeatedly removes one element at a time using either the remove()
or removeChild()
method.
Options Compared
There are two options being compared:
remove()
: This method is used to remove the last child element from the DOM. It sets the length of the childNodes array to 0, which also removes all child elements.removeChild()
: This method is used to remove a single element from the DOM by taking it out of its parent node's children array.Pros and Cons
remove()
: Pros:removeChild()
: Pros:Library and Special JS Feature
There are no libraries used in this benchmark. The test case relies on built-in JavaScript methods (remove()
and removeChild()
).
The test case does not use any special JavaScript features like ES6 or modern syntax.
Other Considerations
Alternatives
Other alternatives for removing elements from an HTML document include:
parentNode.removeChild()
: This method is similar to removeChild()
but takes a specific element as an argument.Element.remove()
: This method is part of the W3C standard for web components and allows removing elements in a more declarative way.Array.prototype.splice()
or other array methods to remove elements from an array, which can be used to store child elements before removal.Keep in mind that each approach has its own trade-offs and use cases, and the best choice depends on the specific requirements of your project.