var e1 = document.createElement('div');
var e2 = document.createElement('div');
var e3 = document.createElement('div');
var e4 = document.createElement('div');
var es = [e1, e2, e3, e4];
document.body.append(e1, e2, e3, e4);
document.body.appendChild(e1);
document.body.appendChild(e2);
document.body.appendChild(e3);
document.body.appendChild(e4);
document.body.append.apply(document.body, es);
for (var i = 0; i < es.length; i++) document.body.appendChild(es[i]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
append() | |
appendChild() | |
append() with apply() | |
appendChild() with loop |
Test name | Executions per second |
---|---|
append() | 531419.8 Ops/sec |
appendChild() | 565911.6 Ops/sec |
append() with apply() | 524588.2 Ops/sec |
appendChild() with loop | 483389.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of two approaches for adding multiple elements to an HTML document:
ParentNode.append()
Node.appendChild()
(with various iterations)We'll explore each approach, its pros and cons, and other considerations.
ParentNode.append()
ParentNode.append()
is a more convenient method for appending elements, as it takes advantage of the optimized internal node list implementation in modern browsers. This method allows for faster and more efficient append operations, especially when dealing with large numbers of elements.
Pros:
Cons:
Node.appendChild()
The appendChild()
method is a standard JavaScript API for appending elements. It's widely supported across different browsers and versions.
Pros:
Cons:
ParentNode.append()
Append() with apply()
Using apply()
on the append()
method allows for more flexible and dynamic appending of multiple elements. This approach enables iteration over an array of elements, which can be useful in certain scenarios.
Pros:
Cons:
ParentNode.append()
Append() with loop
Using a traditional for
loop to iterate through an array of elements and call appendChild()
on each element is another approach. This method provides more control over the append operation.
Pros:
Cons:
ParentNode.append()
Library
The benchmark doesn't explicitly mention a specific library, but it does use the document
object and its methods (appendChild()
and append()
). The document
object is a built-in part of the HTML DOM (Document Object Model) in JavaScript.
Special JS feature or syntax
There are no special JavaScript features or syntaxes mentioned in this benchmark. However, modern browsers have implemented various features and APIs to improve performance, such as:
These features might impact the performance of the benchmark results, but they are not directly related to the methods being compared.
Alternatives
For those interested in exploring alternative approaches or testing other methods, here are a few suggestions:
insertBefore()
: Instead of appending elements, you could use insertBefore()
to insert elements before an existing element.div
instead of body
) and observe the performance differences.Keep in mind that these alternatives might introduce additional complexity or variations in the results, so it's essential to carefully evaluate their impact on your specific use case.