<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
document.body.innerHtml = ''
const range = document.createRange();
const documentFragment = range.createContextualFragment(`
<div>
<label class="clickAndCollect-label" id="click-and-collect">
<div class="clickAndCollect-input-container">
<input class="clickAndCollect-input" type="checkbox" value="click-and-collect">
<span class="clickAndCollect-label-text">Entregarme el pedido hasta mi vehiculo JS</span>
</div>
</label>
</div>
`);
document.body.appendChild(documentFragment);
const el = $(`
<div>
<label class="clickAndCollect-label" id="click-and-collect">
<div class="clickAndCollect-input-container">
<input class="clickAndCollect-input" type="checkbox" value="click-and-collect">
<span class="clickAndCollect-label-text">Entregarme el pedido hasta mi vehiculo JQ</span>
</div>
</label>
</div>
`);
$('body').append(el);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JS | |
JQUERY |
Test name | Executions per second |
---|---|
JS | 1826.8 Ops/sec |
JQUERY | 157.4 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons.
Benchmark Overview
The provided benchmark uses JSON data to define two test cases: one using JavaScript (JS) and another using jQuery (JQUERY). The goal of this benchmark is to compare the execution performance of these two approaches on a specific HTML document structure.
Script Preparation Code
Before running each test case, the script preparation code sets up the environment by clearing the innerHTML
property of the document.body
. This ensures that any previous content or state is reset before the benchmark runs.
The HTML preparation code includes a jQuery library reference with a valid integrity token, which is used to verify the authenticity and integrity of the downloaded JavaScript file.
Test Cases
There are two test cases:
document.createRange()
and createContextualFragment()
methods to create an HTML document fragment.$
function to parse the same HTML template and append it to the body
element.Comparison
The benchmark compares the execution performance of these two approaches on a specific HTML structure, measuring the number of executions per second (ExecutionsPerSecond).
Pros and Cons
Here are some pros and cons associated with each approach:
Library Usage
jQuery is used in both test cases. Its $
function is used to parse the HTML template and append it to the body
element. jQuery's role is to provide a convenient interface for DOM manipulation, which can simplify development but may add complexity to benchmarking.
Special JavaScript Feature/Syntax
This benchmark does not specifically use any unique or specialized JavaScript features or syntax.
Alternatives
Other alternatives for similar benchmarking scenarios could include:
Keep in mind that the choice of alternative benchmarking scenarios depends on the specific requirements and goals of the project.