Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS: insertBefore vs appendChild vs prepend vs insertAdjacentElement
(version: 0)
Comparing performance of:
insertBefore vs appendChild vs prepend vs insertAdjacentElement
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="container"></div>
Script Preparation code:
var container = document.getElementById("container"); var paragraph = document.createElement("p");
Tests:
insertBefore
container.insertBefore(paragraph, null);
appendChild
container.appendChild(paragraph);
prepend
container.prepend(paragraph);
insertAdjacentElement
container.insertAdjacentElement('afterbegin', paragraph);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
insertBefore
appendChild
prepend
insertAdjacentElement
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
26 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
insertBefore
1829346.5 Ops/sec
appendChild
1966058.5 Ops/sec
prepend
1728166.9 Ops/sec
insertAdjacentElement
1826948.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance of different approaches to insert an HTML element (`paragraph`) into a container element (`container`) on a web page. The approaches are: 1. `insertBefore` 2. `appendChild` 3. `prepend` 4. `insertAdjacentElement` **Script Preparation Code and Html Preparation Code** The script preparation code creates a variable `container` by selecting an HTML element with the id "container" from the document, and another variable `paragraph` by creating a new `<p>` HTML element. The html preparation code creates a basic HTML structure containing a single `<div>` element with the id "container". **Individual Test Cases** Each test case defines a specific benchmarking scenario using one of the four approaches: 1. `insertBefore`: inserts the paragraph element after a specified index (in this case, null). 2. `appendChild`: appends the paragraph element to the end of the container. 3. `prepend`: prepends the paragraph element to the beginning of the container. 4. `insertAdjacentElement`: inserts the paragraph element at a specific position relative to the container's parent node (in this case, afterbegin). **Library and Special JS Features** There are no libraries used in this benchmarking framework. **Options Compared** The options compared are: * `insertBefore` vs `appendChild` vs `prepend` vs `insertAdjacentElement` Each of these approaches has its own characteristics: * **Performance**: Generally, appending to the end of a container is faster than inserting at specific positions using `insertAdjacentElement`. Inserting before or prepending can be slower due to the need to navigate and potentially reorder elements. * **Memory Allocation**: Some browsers might allocate more memory for containers with many elements inserted at specific positions (e.g., `insertAdjacentElement`). * **Browser Support**: `insertAdjacentElement` is a relatively new feature, introduced in Chrome 83. Older browsers might not support it. **Pros and Cons of Each Approach** Here's a brief summary: * `insertBefore`: Pros: more control over element ordering; Cons: potentially slower performance due to reordering. * `appendChild`: Pros: faster performance, less memory allocation; Cons: limited control over element ordering. * `prepend`: Similar to `insertBefore`, but prepends the element instead of inserting after. * `insertAdjacentElement`: Pros: flexible positioning; Cons: relatively new feature, potentially slower performance, and more memory allocation. **Other Alternatives** If you need to insert elements into a container, consider using: * `splice()`: A native JavaScript method that can be used with arrays or DOM elements. It's similar to `insertAdjacentElement` but provides more control. * `DocumentFragment`: A lightweight element that allows appending multiple elements without triggering a reflow. When choosing an approach, consider the specific requirements of your project, such as performance, memory allocation, and browser support. In conclusion, this benchmarking framework helps compare the performance of different approaches to inserting HTML elements into containers. By understanding the pros and cons of each option, developers can make informed decisions about which method to use in their projects.
Related benchmarks:
lodash assign vs object.assign vs spread
JavaScript spread operator vs Object.assign performance - Kien Nguyen
queryselector vs getelementbyid with classes and ids
While loop parentElement vs closest (vanilla javascript)
array.from.map vs array.from with map
Comments
Confirm delete:
Do you really want to delete benchmark?