<ul id="theList">
<li class="odd">Item 1</li>
<li class="even">Item 2</li>
<li class="odd">Item 3</li>
<li class="even">Item 4</li>
<li class="odd">Item 5</li>
<li class="even">Item 6</li>
<li class="odd">Item 7</li>
<li class="even">Item 8</li>
<li class="odd">Item 9</li>
<li class="even">Item 10</li>
<li class="odd">Item 11</li>
<li class="even">Item 12</li>
<li class="odd">Item 13</li>
<li class="even">Item 14</li>
<li class="odd">Item 15</li>
<li class="even">Item 16</li>
<li class="odd">Item 17</li>
<li class="even">Item 18</li>
<li class="odd">Item 19</li>
<li class="even">Item 20</li>
<li class="odd">Item 21</li>
<li class="even">Item 22</li>
<li class="odd">Item 23</li>
<li class="even">Item 24</li>
<li class="odd">Item 25</li>
<li class="even">Item 26</li>
<li class="odd">Item 27</li>
<li class="even">Item 28</li>
<li class="odd">Item 29</li>
<li class="even">Item 30</li>
</ul>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
var el = document.getElementById('theList');
var html = $(el).html();
var html = el.innerHTML;
$(el).html('<li>Item A</li><li>Item B</li>');
el.innerHTML = '<li>Item A</li><li>Item B</li>';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery: get html | |
get innerHTML | |
jQuery: set html | |
set innerHTML |
Test name | Executions per second |
---|---|
jQuery: get html | 257114.6 Ops/sec |
get innerHTML | 274826.3 Ops/sec |
jQuery: set html | 482848.1 Ops/sec |
set innerHTML | 1001927.7 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Overview
This benchmark compares the performance of two approaches to set or get HTML content on an HTML element: jQuery's .html()
method versus the innerHTML
property directly on the DOM element.
Options Compared
Two options are compared:
.html()
method: This is a wrapper around the innerHTML
property, but with additional functionality and features that come with using a library like jQuery.innerHTML
: This option simply uses the innerHTML
property of the DOM element to set or get HTML content.Pros and Cons
.html()
methodPros:
Cons:
innerHTML
, which can lead to slower performance compared to direct access.innerHTML
Pros:
Cons:
.html()
method.Other Considerations
.html()
method wraps around the innerHTML
property, but also adds additional features like automatic string normalization.innerHTML
. In this benchmark, all tests are run on Chrome 125, which supports modern web standards.Alternative Approaches
Other approaches to set or get HTML content include:
textContent
property instead of innerHTML
, which can be more efficient for large blocks of text.In summary, this benchmark compares the performance of two approaches to set or get HTML content on an HTML element: jQuery's .html()
method versus directly accessing innerHTML
. While jQuery's approach provides a more convenient API, it may introduce additional overhead due to its wrapper around innerHTML
. The direct approach is faster but requires more boilerplate code.