<div id="container"></div>
<div id='p1'></div>
<div id='p2'></div>
<div id='p3'></div>
<div id='p4'></div>
<div id='p5'></div>
var container = document.getElementById("container");
var p1 = document.getElementById("p1");
var p2 = document.getElementById("p2");
var p3 = document.getElementById("p3");
var p4 = document.getElementById("p4");
var p5 = document.getElementById("p5");
container.append(p1, p2, p3, p4, p5);
container.appendChild(p1);
container.appendChild(p2);
container.appendChild(p3);
container.appendChild(p4);
container.appendChild(p5);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
append | |
appendChild |
Test name | Executions per second |
---|---|
append | 113943.5 Ops/sec |
appendChild | 106550.2 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of two approaches for inserting multiple elements into an HTML container: append
and appendChild
. The test simulates adding five child elements to the container using these methods.
Options Compared
Two options are compared:
container.append(p1, p2, p3, p4, p5);
: This approach uses the append
method on the container element, passing an array of child elements as arguments.container.appendChild(p1);\r\ncontainer.appendChild(p2);\r\ncontainer.appendChild(p3);\r\ncontainer.appendChild(p4);\r\ncontainer.appendChild(p5);
: This approach uses the appendChild
method on the container element, appending each child element individually.Pros and Cons
append
Method:appendChild
Method:Library Used
None. This benchmark is a native JavaScript test, not relying on any external libraries.
Special JS Features/Syntax
The benchmark uses the append
and appendChild
methods, which are part of the DOM (Document Object Model) API. These methods are widely supported across modern browsers.
Alternative Approaches
Other approaches could be considered:
Benchmark Code Explanation
The provided Script Preparation Code
initializes variables for each child element and the container, ensuring that they are available when the benchmark tests are executed.
The Html Preparation Code
defines the HTML structure for the test, including the container and individual elements. This code is used by both test cases to establish a common baseline.
Test Case Explanation
Each test case executes a different approach for inserting multiple child elements into the container:
append
Test: Uses the append
method on the container element, passing an array of child elements as arguments.appendChild
Test: Uses the appendChild
method on the container element, appending each child element individually.The benchmark measures the performance (number of executions per second) for both test cases using different browsers and devices.
Understanding Benchmark Results
The latest benchmark results show the execution frequency (executions per second) for each test case across various browsers. The append
method generally outperforms the appendChild
approach, likely due to its batching mechanism.