multiple option selected (v3)

Revision 3 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<select id="sel" size="3" multiple="multiple" name="sel[]">
        <option selected="selected" value="-1">---</option>
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="3">C</option>
        <option value="4">D</option>
        <option value="5">E</option>
        <option value="6">F</option>
        <option value="7" selected="selected">G</option>
        <option value="8">H</option>
        <option value="9">I</option>
        <option value="10">J</option>
        <option value="11">K</option>
        <option value="12">L</option>
        <option value="13" selected="selected">M</option>
</select>

Setup

var targetSelect = document.getElementById('sel');

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
b = $( targetSelect ).val().length > 1;
ready
Native
                        var aOptions = targetSelect.options,
                                nIndex = aOptions.length,
                                nCount = 0, b;


                        while ( nIndex-- && nCount <= 1 )
                        {
                                if( aOptions[nIndex].selected )
                                {
                                        nCount++;
                                }
                        }

                        b = nCount > 1;
ready
jQuery 2
b = $("option:selected", targetSelect).length > 1;
ready
QSL selected
b = targetSelect.querySelectorAll('option[selected=selected]').length > 1;
ready
SelectedOptions
b = targetSelect.selectedOptions.length > 1
ready
jQuery 3
b = $("option[selected=selected]", targetSelect).length > 1;
ready
Reduce
b = targetSelect.options.reduce(function(prev, current, index, array) {
  return prev + (current.selected ? 1 : 0);
}, 0) > 1;
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.