function createFragment(strHTML) {
return document.createRange().createContextualFragment(strHTML);
}
function createElement(tagName, props) {
const element = document.createElement(tagName);
if (props) {
if (typeof props === 'string') {
element.className = props;
}
else {
for (const key in props) {
element[key] = props[key];
}
}
}
return element;
}
createFragment(`
<div class="eventList">
<h3 class="Titleevent">
<span class="TitleeventLabel">Evento: </span>
<span class="TitleeventId">${'id'} - </span>
<span class="TitleeventName">${'name'}</span>
</h3>
<div class="mensagemlist">
<div class="mensagemlistBox">
<label class="mensagemlistBoxLabel">Deje un mensaje:</label>
<textarea class="mensagemlistBoxInput" id="mensajeLimit"></textarea>
<small id="sprestante"></small>
</div>
<div class="mensagemlistButtons">
<input type="submit" value="Guardar" class="btn-guardar">
<button class="btn-excluir">Esta compra no es un regalo</button>
</div>
</div>
</div>
`)
const title = createElement('h3', { className: 'Titleevent' });
title.appendChild(createElement('span', { className: 'TitleeventLabel', textContent: 'Evento: ' }));
title.appendChild(createElement('span', { className: 'TitleeventId', textContent: `${'id'} - ` }));
title.appendChild(createElement('span', { className: 'TitleeventName', textContent: 'name' }));
const messageList = createElement('div', { className: 'mensagemlist' });
const listBox = createElement('div', { className: 'mensagemlistBox' });
listBox.appendChild(createElement('label', { className: 'mensagemlistBoxLabel', textContent: 'Deje un mensaje:' }));
listBox.appendChild(createElement('textarea', { className: 'mensagemlistBoxInput', id: 'mensajeLimit' }));
listBox.appendChild(createElement('small', { id: 'sprestante' }));
const buttons = createElement('div', { className: 'mensagemlistButtons' });
buttons.appendChild(createElement('input', { type: 'submit', value: 'Guardar', className: 'btn-guardar' }));
buttons.appendChild(createElement('button', { className: 'btn-excluir', textContent: 'Esta compra no es un regalo' }));
messageList.appendChild(listBox);
messageList.appendChild(buttons);
const container = createElement('div', 'eventList')
container.appendChild(title);
container.appendChild(messageList);
createFragment(`
<div class="eventList">
teste
</div>
`)
createElement('div', { className: 'eventList', textContent: 'teste' })
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createFragment | |
createElement | |
createFragment Small | |
createElement Small |
Test name | Executions per second |
---|---|
createFragment | 11658.9 Ops/sec |
createElement | 33220.9 Ops/sec |
createFragment Small | 318924.0 Ops/sec |
createElement Small | 1102199.6 Ops/sec |
I'll break down the provided benchmark definition and test cases, explaining what's being tested, the different approaches, their pros and cons, and other considerations.
Benchmark Definition
The benchmark definition consists of two main functions: createFragment
and createElement
. The latter is a general-purpose function for creating HTML elements, while the former creates a fragment of an HTML document.
createFragment
This function uses the document.createRange().createContextualFragment(strHTML)
method to create a fragment of an HTML document. The input string strHTML
contains the HTML code to be included in the fragment. This approach allows for efficient rendering of HTML content without having to parse and render the entire document.
createElement
This function creates individual HTML elements using the document.createElement(tagName)
method. It takes two arguments: tagName
(the element's tag name) and an optional object props
containing attribute values or CSS styles. The function returns a newly created element.
Options Compared
The benchmark compares three different approaches:
<div>
element, containing a single text node ("teste").createElement
, including its own children (a span with "teste" text content).Pros and Cons
createFragment:
createElement:
createFragment Small and createElement Small:
Other Considerations
document
object to create and manipulate elements, which is a fundamental part of the DOM API.Alternatives
Other approaches for creating HTML elements or fragments might include:
Keep in mind that this benchmark is likely focused on evaluating the performance of browser rendering and execution speed, rather than exploring alternative approaches for creating HTML elements or fragments.