<div id="container"></div>
var container = document.getElementById("container");
var paragraph = document.createElement("p");
var paragraph1 = document.createElement("p");
var paragraph2 = document.createElement("p");
var paragraph3 = document.createElement("p");
container.append(paragraph,paragraph1,paragraph2,paragraph3);
container.appendChild(paragraph);
container.appendChild(paragraph1);
container.appendChild(paragraph2);
container.appendChild(paragraph3);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
append | |
appendChild |
Test name | Executions per second |
---|---|
append | 10673.6 Ops/sec |
appendChild | 11661.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two ways to append multiple elements to an HTML container using JavaScript: container.append()
with multiple arguments (known as "polyfill" behavior) versus container.appendChild()
. This test is focused on measuring the performance difference between these two approaches.
Options Compared
There are two options being compared:
append()
method with multiple arguments: This method uses a polyfill to handle multiple elements by concatenating them into an array and then appending that array to the container.appendChild()
method: This is the standard method for adding a single element to an HTML container.Pros and Cons of Each Approach
append()
method with multiple arguments:
Pros:
Cons:
append()
(but this is already handled by polyfills)appendChild()
method:
Pros:
append()
methodCons:
appendChild()
multiple times for each elementOther Considerations
The test also considers the impact of polyfilling the append()
method on performance. In older browsers, a polyfill would be used to provide support for the append()
method, which might incur some overhead.
Library and Special JS Feature
There is no explicit library mentioned in the benchmark definition or results. However, it's worth noting that modern browsers like Firefox already include a polyfill for the append()
method to ensure compatibility with older browsers.
Test Case Explanation
The test case consists of two individual tests:
append()
method: This test creates four paragraphs and appends them to the container using the append()
method with multiple arguments.appendChild()
method: This test creates four paragraphs and appends them to the container using separate calls to appendChild()
for each paragraph.Alternative Test Cases
Other alternatives might include:
insertAdjacentHTML()
or createElementAndInsert()
.append()
method.These alternative test cases could help developers understand the performance implications of using different approaches to adding elements to an HTML container, and inform decisions about which approach is best suited for their specific use case.