<ul id="theList">
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
var els = $('#theList li');
els.each(function() {
$(this).text('Changed');
});
var els = document.querySelectorAll('#theList li');
Array.prototype.slice.call(els).forEach(function(el) {
el.textContent = 'Changed';
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery | |
Vanilla JS |
Test name | Executions per second |
---|---|
jQuery | 5456.3 Ops/sec |
Vanilla JS | 73413.6 Ops/sec |
Benchmark Overview
The MeasureThat.net benchmark compares the performance of two approaches for changing the text of list items: jQuery and vanilla JavaScript.
Options Compared
Two options are compared:
document.querySelectorAll()
, Array.prototype.forEach()
) without any additional libraries.Pros and Cons
Pros:
Cons:
Pros:
Cons:
Library and Syntax Used
The benchmark uses the following libraries and syntax features:
document.querySelectorAll()
, Array.prototype.forEach()
) without any additional libraries.Special JS Feature or Syntax
No special features or syntax are used in this benchmark, as both approaches rely on standard JavaScript methods. However, the use of jQuery introduces some additional complexity due to its own syntax and behavior.
Alternatives
For changing the text of list items, alternative approaches could be:
Keep in mind that the choice of approach depends on the specific project requirements, team expertise, and personal preferences.