<select>
<option value="Strawberry">Strawberry</option>
<option value="Blueberry">Blueberry</option>
<option value="Peach">Peach</option>
<option value="Pomegranate">Pomegranate</option>
<option value="Grape">Grape</option>
<option value="Banana">Banana</option>
<option value="Cherry">Cherry</option>
<option value="Pineapple">Pineapple</option>
<option value="Pear">Pear</option>
<option value="Plum">Plum</option>
<option value="Muskmelon">Muskmelon</option>
<option value="Avocado">Avocado</option>
<option value="Apricot">Apricot</option>
<option value="Mango">Mango</option>
<option value="Apple">Apple</option>
<option value="Papaya">Papaya</option>
<option value="Lemon">Lemon</option>
<option value="Coconut">Coconut</option>
<option value="Jackfruit">Jackfruit</option>
<option value="Nectarine">Nectarine</option>
<option value="Strawberry">Strawberry</option>
<option value="Blueberry">Blueberry</option>
<option value="Peach">Peach</option>
<option value="Pomegranate">Pomegranate</option>
<option value="Grape">Grape</option>
<option value="Green Banana">Green Banana</option>
<option value="Cherry">Cherry</option>
<option value="Pineapple">Pineapple</option>
<option value="Pear">Pear</option>
<option value="Plum">Plum</option>
<option value="Muskmelon">Muskmelon</option>
<option value="Avocado">Avocado</option>
<option value="Apricot">Apricot</option>
<option value="Mango">Mango</option>
<option value="Apple">Apple</option>
<option value="Papaya">Papaya</option>
<option value="Lemon">Lemon</option>
<option value="Coconut">Coconut</option>
<option value="Jackfruit">Jackfruit</option>
<option value="Nectarine">Nectarine</option>
</select>
window.options = [
{ text: 'Strawberry', value: 'Strawberry', selected: false },
{ text: 'Blueberry', value: 'Blueberry', selected: false },
{ text: 'Peach', value: 'Peach', selected: false },
{ text: 'Pomegranate', value: 'Pomegranate', selected: false },
{ text: 'Grape', value: 'Grape', selected: false },
{ text: 'Blueberry', value: 'Banana', selected: false },
{ text: 'Cherry', value: 'Cherry', selected: false },
{ text: 'Pineapple', value: 'Pineapple', selected: false },
{ text: 'Pear', value: 'Pear', selected: false },
{ text: 'Plum', value: 'Plum', selected: false },
{ text: 'Muskmelon', value: 'Muskmelon', selected: false },
{ text: 'Avocado', value: 'Avocado', selected: false },
{ text: 'Apricot', value: 'Apricot', selected: false },
{ text: 'Mango', value: 'Mango', selected: false },
{ text: 'Apple', value: 'Apple', selected: false },
{ text: 'Papaya', value: 'Papaya', selected: false },
{ text: 'Lemon', value: 'Lemon', selected: false },
{ text: 'Coconut', value: 'Coconut', selected: false },
{ text: 'Jackfruit', value: 'Jackfruit', selected: false },
{ text: 'Nectarine', value: 'Nectarine', selected: false },
{ text: 'Strawberry', value: 'Strawberry', selected: false },
{ text: 'Blueberry', value: 'Blueberry', selected: false },
{ text: 'Peach', value: 'Peach', selected: false },
{ text: 'Pomegranate', value: 'Pomegranate', selected: false },
{ text: 'Grape', value: 'Grape', selected: false },
{ text: 'Green Banana', value: 'Green Banana', selected: false },
{ text: 'Cherry', value: 'Cherry', selected: false },
{ text: 'Pineapple', value: 'Pineapple', selected: false },
{ text: 'Pear', value: 'Pear', selected: false },
{ text: 'Plum', value: 'Plum', selected: false },
{ text: 'Muskmelon', value: 'Muskmelon', selected: false },
{ text: 'Avocado', value: 'Avocado', selected: false },
{ text: 'Apricot', value: 'Apricot', selected: false },
{ text: 'Mango', value: 'Mango', selected: false },
{ text: 'Apple', value: 'Apple', selected: false },
{ text: 'Papaya', value: 'Papaya', selected: false },
{ text: 'Lemon', value: 'Lemon', selected: false },
{ text: 'Coconut', value: 'Coconut', selected: false },
{ text: 'Jackfruit', value: 'Jackfruit', selected: false },
{ text: 'Nectarine', value: 'Nectarine', selected: false }
]
window.options.find(o => o.value === 'Green Banana');
const selected = [document.querySelector('select').options];
selected.find(s => s.value === 'Green Banana');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Variable | |
Elements |
Test name | Executions per second |
---|---|
Variable | 6877897.5 Ops/sec |
Elements | 562589.0 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Description: The benchmark is designed to compare two approaches for searching a specific element within an array of objects in JavaScript:
find
method on an array, passing a callback function that checks if the current element's value matches the target value ('Green Banana'
). The first occurrence of the match returns the index of the element....
) to convert the select
HTML element into an array of options, and then finds the specific option using the same callback function as in the Variable approach.Options: The two main options being compared are:
find
method on an array (Variable approach)Latest Benchmark Result: The latest benchmark result shows that the Mobile Safari 14 browser with iOS 14.0.1 operating system achieves higher execution rates for both test cases:
Analysis:
select
element to an array and finding the specific option within that array.In general, when working with arrays and performing search operations, the find
method on an array tends to be faster than using the spread operator to convert a DOM element into an array. However, in this specific case, the difference is relatively small, and the performance gain from using the find
method may not be significant for all use cases.
It's worth noting that the benchmark results might vary depending on the specific test environment, hardware, and other factors.