JQuery: find with selected selector vs filter selected selector
find the best solution for optimize getting selected option
Date tested:
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
Benchmark definition (click to collapse):
HTML Preparation code:
<select id="items"> <option value="1"></option> <option value="2"></option> <option value="3"></option> <option value="4"></option> <option value="5"></option> <option value="6"></option> <option value="7"></option> <option value="8"></option> <option value="9"></option> <option value="10"></option> </select> <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