<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<select id="select">
</select>
var max = 2000;
var select=$("#select");
for(i=0;i<max;i++){
select.append('<option value="'+i+'"></option>');
}
var max = 2000;
for(i=0;i<max;i++){
var option = document.querySelector("#select option[value='"+i+"']");
option.setAttribute('selected',true);
}
var max = 2000;
for(i=0;i<max;i++){
var option = $("#select option[value='"+i+"']");
option.attr('selected',true);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
queryselect | |
jquery |
Test name | Executions per second |
---|---|
queryselect | 8.4 Ops/sec |
jquery | 4.1 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and the pros/cons of different approaches.
Benchmark Definition
The benchmark is designed to compare the performance of two approaches:
document.querySelector
( native JavaScript) to select an option from a <select>
element.$()
function to select an option from a <select>
element.Script Preparation Code
The script preparation code is identical for both test cases and sets up the following:
max
with a value of 2000, which will be used to create multiple options in the <select>
element.<select>
element with an ID of "select".max-1
, appending an option to the <select>
element for each iteration. The option's value is set to the current loop index.Html Preparation Code
The HTML preparation code includes a reference to jQuery library (//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
) and creates a basic <select>
element with an ID of "select".
Individual Test Cases
There are two test cases:
queryselect
( native JavaScript)This test case uses the document.querySelector
function to select the option from the <select>
element.
var max = 2000;
for(i=0;i<max;i++) {
var option = document.querySelector("#select option[value='"+i+"']");
option.setAttribute('selected',true);
}
Pros and Cons:
Pros:
<select>
element.Cons:
document
object and navigating DOM elements.jquery
This test case uses jQuery's $()
function to select the option from the <select>
element.
var max = 2000;
for(i=0;i<max;i++) {
var option = $( "#select option[value='"+i+"']" );
option.attr('selected',true);
}
Pros and Cons:
Pros:
Cons:
Library:
In this benchmark, jQuery is used as a third-party library. Its purpose is to simplify DOM manipulation and provide a consistent way to select elements across different browsers and platforms.
Special JS Feature/Syntax:
This benchmark uses the let
keyword with block scope, which was introduced in ECMAScript 2015 (ES6). It's not explicitly mentioned as being tested or compared, but it's an important feature that might affect performance in certain situations.
Other Alternatives:
<select>
elements.document.getElementsByTagName()
instead of querySelector
.Keep in mind that this benchmark is specifically designed to compare the performance of native JavaScript and jQuery's $()
function. The results will provide insights into which approach is faster and more efficient for this particular use case.