<div id="testdiv">
<div class="unique">test</div>
</div>
var i, imax;
var div = document.getElementById('testdiv');
var newDiv = document.createElement('div');
newDiv.classList.add('unique');
newDiv.innerText = 'test';
div.appendChild(newDiv);
var newDiv = `<div class="unique">test</div>`;
div.innerHTML = newDiv;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
template string |
Test name | Executions per second |
---|---|
createElement | 657759.5 Ops/sec |
template string | 656892.6 Ops/sec |
Let's dive into the benchmark and explain what's being tested, compared, and the pros/cons of each approach.
Benchmark Context The benchmark is comparing two ways to create HTML elements in JavaScript:
document.createElement
(createElement)innerHTML
(template string)What's Being Tested?
Options Compared
The benchmark is comparing the performance of two alternatives:
document.createElement
and setting its properties (class and text content) individually.Pros and Cons of Each Approach
Pros:
Cons:
Pros:
Cons:
Library/Dependency
The benchmark uses JavaScript's built-in document
object, which is a part of the DOM (Document Object Model). This object provides an API to interact with HTML documents and create new elements.
Special JS Feature/Syntax
The benchmark uses template literals (<div class="unique">test</div>
) which are a feature introduced in ECMAScript 2015 (ES6) as part of the JavaScript language specification. Template literals provide a concise way to embed expressions inside string literals, making it easier to create HTML elements.
Other Alternatives
If you're interested in exploring alternative approaches, here are some options:
Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case and requirements.