const row = '<tr><td>1</td><td>2</td></tr>';
const table = '<table><tbody>' + Array(10000).fill(row) + '</tbody></table>';
const template = document.createElement('template');
template.innerHTML = table;
const div = document.createElement('div');
template.content.firstChild.cloneNode(true);
const row = '<tr><td>1</td><td>2</td></tr>';
const table = '<table><tbody>' + Array(10000).fill(row) + '</tbody></table>';
const template = document.createElement('template');
template.innerHTML = table;
const div = document.createElement('div');
div.innerHTML = table;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
CloneNode | |
InnerHTML |
Test name | Executions per second |
---|---|
CloneNode | 89.1 Ops/sec |
InnerHTML | 40.3 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Definition
The benchmark is comparing two approaches to populate the inner HTML of an element: CloneNode
and InnerHTML
. The script preparation code and HTML preparation code are empty, which means that the benchmark doesn't generate any specific HTML or JavaScript setup. This allows users to focus solely on the differences between these two approaches.
Options Compared
The two options being compared are:
cloneNode(true)
. The clone is then used as the inner HTML of another element.innerHTML
property.Pros and Cons
innerHTML
property.Library Used
There is no explicit library mentioned in the benchmark definition. However, it's likely that the innerHTML
property is implemented by a web browser-specific API (e.g., Chromium) and not a standard JavaScript library.
Special JS Feature/Syntax
None of the test cases use any special JavaScript features or syntax beyond the basic usage of cloneNode
and innerHTML
.
Other Alternatives
If you're interested in exploring other approaches, consider these alternatives:
CloneNode
or InnerHTML
, you could use DOM traversal methods like querySelectorAll()
or getElementsByClassName()
to select elements and then update their content.Keep in mind that these alternatives might have different performance characteristics and are suitable for specific use cases only.