Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Replace vs Set
(version: 1)
Hydra Test
Comparing performance of:
Set Attribute vs Replace Element
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div><svg id="target" class="target"></svg></div>
Script Preparation code:
const target = document.getElementById('target'); var div = document.createElement('DIV'); div.innerHTML = `<svg id="target" class="something new"></svg>`; var replacement = div.firstChild;
Tests:
Set Attribute
var i = 1000; while (i--) { target.setAttribute('class', 'something new') }
Replace Element
var i = 1000; while (i--) { target.parentNode.replaceChild(replacement, target); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set Attribute
Replace Element
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):
I'd be happy to explain the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark compares two approaches: setting an attribute (`Set Attribute`) and replacing an element using `replaceChild` ((`Replace Element`). The goal is to determine which approach is faster. **Test Cases** There are two test cases: 1. **Set Attribute**: The script sets the `class` attribute of the `target` element 1000 times in a loop. ```javascript var i = 1000; while (i--) { target.setAttribute('class', 'something new'); } ``` 2. **Replace Element**: The script replaces the `target` element with a new child element (`replacement`) 1000 times in a loop. ```javascript var i = 1000; while (i--) { target.parentNode.replaceChild(replacement, target); } ``` **Library Usage** The benchmark uses the `DOM` library to interact with the DOM. Specifically, it uses `document.getElementById`, `createElement`, `setAttribute`, and `replaceChild`. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Benchmark Results** The latest results show that: * The **Set Attribute** test runs at an average of 829 executions per second (executionsPerSecond). * The **Replace Element** test runs at an average of 451 executions per second (executionsPerSecond). **Pros and Cons of Each Approach** 1. **Set Attribute**: * Pros: Typically faster, as it only updates a single property. * Cons: May not be suitable for updating multiple attributes or nested properties. 2. **Replace Element**: * Pros: Suitable for replacing elements with new content or styles. * Cons: Can be slower, especially when dealing with large DOM trees. **Other Alternatives** In addition to the two approaches mentioned above, other alternatives for setting or replacing element properties include: * Using a CSS style sheet to update styles (e.g., `target.style.class = 'something new'`). * Using a library like jQuery to simplify DOM manipulation. * Using a more modern approach like using a MutationObserver to observe changes to the DOM. It's worth noting that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
Hydra Test
replaceChildren vs textContent + append
replaceChildren vs documentFragment [2]
clear element using replaceChildren vs removeChild
Comments
Confirm delete:
Do you really want to delete benchmark?