Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Replace text content on DOM element 2
(version: 1)
Comparing performance of:
innerHTML vs textContent vs replaceChildren vs remove & append
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id='target'></div>
Script Preparation code:
let el = document.getElementById("target"); for(i=0; i<10; i++) { el.innerHTML += "<span>Text</span>" }
Tests:
innerHTML
let el = document.getElementById("target"); el.innerHTML = "Some ordinary text"
textContent
let el = document.getElementById("target"); el.textContent = "Some ordinary text"
replaceChildren
let el = document.getElementById("target"); let txt = document.createTextNode("Some ordinary text") el.replaceChildren(txt);
remove & append
let el = document.getElementById("target"); while (el.firstChild) { el.removeChild(el.firstChild); } let txt = document.createTextNode("Some ordinary text") el.appendChild(txt);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
innerHTML
textContent
replaceChildren
remove & append
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
innerHTML
1975749.8 Ops/sec
textContent
14454660.0 Ops/sec
replaceChildren
3585514.5 Ops/sec
remove & append
3803472.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and analyze what's being tested. **Benchmark Definition JSON** The benchmark definition JSON represents a single test case, which is to replace text content on a DOM element. The script preparation code is provided by the user, and it uses `document.getElementById` to get a reference to an element with the id "target". The HTML preparation code is also provided, which creates a simple `<div>` element with the specified id. **Individual Test Cases** The individual test cases represent different ways to replace text content on the DOM element. Each test case has a unique script preparation code that uses one of the following methods: 1. `innerHTML`: sets the HTML content of an element 2. `textContent`: sets the text content of an element 3. `replaceChildren`: replaces the children of an element with new nodes, in this case, a single text node **Options Compared** The test cases compare three different approaches to replace text content on the DOM element: 1. Using `innerHTML` 2. Using `textContent` 3. Using `replaceChildren` with a text node **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. **innerHTML**: Pros: * Easy to use, as it only requires setting the HTML content of an element. * Can be used to set both HTML and text content (although this is not necessary in this specific benchmark). Cons: * May introduce unnecessary DOM nodes and overhead due to parsing and rendering the HTML content. * May have security implications if user-inputted data is used. 2. **textContent**: Pros: * More efficient than `innerHTML`, as it only sets the text content of an element, without introducing additional DOM nodes. Cons: * May require more code to handle cases where both HTML and text content need to be set. 3. **replaceChildren**: Pros: * Highly efficient, as it replaces the entire contents of an element with a new node (in this case, a text node). Cons: * Requires using the `replaceChildren` method, which may not be supported by older browsers. **Library and Purpose** The benchmark uses the `document` object and DOM methods (`getElementById`, `innerHTML`, `textContent`, `replaceChildren`) to interact with the DOM. These are standard JavaScript APIs that come bundled with most browsers. **Special JS Features or Syntax** There are no special JS features or syntax used in this benchmark. It's a straightforward test of different approaches to replace text content on a DOM element. **Other Alternatives** In terms of alternative approaches, there are a few other methods that could be used to replace text content on a DOM element: 1. Using `createElementText` and then using the `appendChild` method 2. Using `document.createDocumentFragment` to create a fragment with the text node and then appending it to the element 3. Using a library like jQuery or React to handle DOM manipulation However, these approaches are not included in this benchmark, as they would add additional complexity and may not be representative of common use cases. I hope this explanation helps!
Related benchmarks:
Test read speed
id vs class test
innerHTML vs textContent vs innerText
textContent vs innerHTML vs innerText
Comments
Confirm delete:
Do you really want to delete benchmark?