Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
innerhtml vs removechild
(version: 0)
Comparing performance of:
innerHTML vs removeChild vs create range
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="container"></div>
Script Preparation code:
var node = document.getElementById('container'); for(var i = 0; i < 1000; i++) node.appendChild(document.createElement('div'));
Tests:
innerHTML
var node = document.getElementById('container'); node.innerHTML = '';
removeChild
var node = document.getElementById('container'); while(node.firstChild) node.removeChild(node.firstChild)
create range
var node = document.getElementById('container'); var r = document.createRange(); r.selectNodeContents(node); r.deleteContents();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
innerHTML
removeChild
create range
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark defines two scripts that are to be executed: 1. `innerHTML`: This script sets the inner HTML of an element (`var node = document.getElementById('container');\nnode.innerHTML = '';`). 2. `removeChild`: This script removes all child nodes from an element (`var node = document.getElementById('container');\nwhile(node.firstChild) node.removeChild(node.firstChild)`). 3. `create range`: This script creates a Range object, selects its contents, and then deletes them (`var node = document.getElementById('container');\nvar r = document.createRange();\nr.selectNodeContents(node);\nr.deleteContents()`). **Options Compared** The benchmark compares the execution time of these three scripts on different nodes: * `innerHTML`: Sets the inner HTML of an element. * `removeChild`: Removes all child nodes from an element. * `create range`: Creates a Range object, selects its contents, and then deletes them. **Pros and Cons** 1. **innerHTML**: This approach is simple and fast but can be inefficient if the element has a lot of content, as it creates multiple text nodes and other DOM elements. 2. **removeChild**: This approach is more efficient than `innerHTML` because it only removes one node at a time, which can reduce memory usage and garbage collection. However, it's slower for large numbers of child nodes. 3. **create range**: This approach is useful when you need to manipulate the contents of an element without modifying its structure, but it requires more memory allocation and can be slower than `removeChild` due to the creation of a Range object. **Library Usage** The benchmark uses the following libraries: * None (no external libraries are used) **Special JS Features or Syntax** None mentioned **Other Considerations** The benchmark also considers the device platform, operating system, and browser version, which can affect execution time. The `ExecutionsPerSecond` value represents the number of executions per second. **Alternatives** If you were to rewrite this benchmark, you might consider adding additional test cases, such as: * Using different HTML elements (e.g., `div`, `span`, `p`) * Adding more child nodes or complex content * Using other methods for manipulating DOM nodes (e.g., `addEventListener`, `innerHTML +=`, etc.) * Testing on different browsers or devices Additionally, you could consider using a benchmarking library like WebPageTest or Benchmark.js to create and run benchmarks.
Related benchmarks:
innerhtml vs removechild vs remove! (few child nodes)
innerhtml vs removeChild-firstChild vs removeChild-lastChild
innerhtml vs innerText vs removechild vs remove!
innerHTML vs removeChild(node.firstChild) vs removeChild(node.lastChild)
innerhtml vs removechild vs remove #0000 (No first child)
Comments
Confirm delete:
Do you really want to delete benchmark?