function testA(){
var div = document.createElement('div');
div.innerText = 'Hello world';
return div;
}
function testB(){
var tmp = document.createElement('div');
var html = '<div>Hello world</div>';
tmp.innerHTML = html;
return tmp.firstChild;
}
var testCData = testA();
function testC(){
return testCData.cloneNode(true);
}
testA();
testB();
testC();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
A | |
B | |
C |
Test name | Executions per second |
---|---|
A | 820908.6 Ops/sec |
B | 166861.1 Ops/sec |
C | 1133336.8 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark, specifically designed to compare the performance of three different approaches: creating an HTML element directly (Test A), using the innerHTML
property to create the content (Test B), and cloning a node with cloneNode(true)
method (Test C).
Options Compared
document.createElement()
method and its innerText
property is set.innerHTML
Property: In Test B, an HTML string is used to create a temporary string with the desired content, which is then assigned to the innerHTML
property of another element (tmp
). The resulting first child of this new element is returned.cloneNode(true)
method on the result of Test A.Pros and Cons
innerHTML
Property (Test B):Library Usage
The cloneNode
method is a part of the DOM API and does not require any specific library. It's a built-in JavaScript function that creates a deep copy of an existing node.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes explicitly mentioned in this benchmark, aside from the use of innerHTML
, which can be considered as using some internal functionality, but its usage is not particularly unique for the scope of this benchmark.