Swap element "in-place" with replaceWith() vs .outerHTML (version: 0)
Swap an DOM element with replaceWith() and compare with the .outerHTML method of replacing its content instead.
replaceWith()
- Pretty one-liner
- Breaks element attributes, inline style and scrollHeight/scrollWidth (fully re-render element)
.outerHTML
- Keeps attributes, inline style and scrollHeight/scrollWidth (partially re-render element)
- Breaks previous element references, "hacky" looking and sometimes perceptible (element may flash for a split second)
Comparing performance of: replaceWith() vs .outerHTML
Memory measurements supported only in Chrome.
For precise memory measurements Chrome must be launched with --enable-precise-memory-info flag.
More information: Monitoring JavaScript Memory
Test case name
Result
replaceWith()
.outerHTML
Fastest:N/A
Slowest:N/A
Latest run results:
Run details: (Test run date:
10 days ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0