set title on options from text

Benchmark created by Gary Green on


Preparation HTML

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

<form id="test"></form>
<script>
  var i = 300,
      str = '';
  while (i--) str += '<option>blah</option>';
  
  $('form').html('<select id="selectTest">' + str + '</select>');
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
old each
$("#test select option").each(function() {
 $(this).attr({
  'title': $(this).text()
 });
});
ready
act directly on elements using each
$('#test select > option').each(function() {
 this.title = this.text;
});
ready
use prop function
$('#test select > option').prop('title', function() {
 return this.text;
});
ready
vanilla js
var options = document.getElementById('selectTest').options,
    i = options.length;

while (--i > -1) options[i].title = options[i].text;
ready
attr function
$('#test select > option').attr('title', function() {
 return this.text;
});
ready

Revisions

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

  • Revision 1: published by Gary Green on