JQuery: find with selected selector vs filter selected selector

find the best solution for optimize getting selected option
4 years ago
User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0
Test name Executions per second
with selected selector 87193.2 Ops/sec
with selected selector in filter 91896.0 Ops/sec
HTML Preparation code:
x
 
1
<select id="items">
2
  <option value="1"></option>
3
  <option value="2"></option>
4
  <option value="3"></option>
5
  <option value="4"></option>
6
  <option value="5"></option>
7
  <option value="6"></option>
8
  <option value="7"></option>
9
  <option value="8"></option>
10
  <option value="9"></option>
11
  <option value="10"></option>
12
</select>
13
14
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js'></script>
Script Preparation code:
 
var $itemsElement = $("#items");
$itemsElement.val("5");
Tests:
  • with selected selector

     
    $itemsElement.find("option:selected");
  • with selected selector in filter

     
    $itemsElement.find("option").filter(":selected");
Open this result on MeasureThat.net