Skip to content

Instantly share code, notes, and snippets.

@redroot
Created September 22, 2011 09:13
Show Gist options
  • Select an option

  • Save redroot/1234393 to your computer and use it in GitHub Desktop.

Select an option

Save redroot/1234393 to your computer and use it in GitHub Desktop.
Different attempts to find order of visible elements in a form with jQuery
var f = $("#form");
// attempt 1: average abbout 2.621 seconds
(function(){
i = 0;
j = 0;
start = new Date().getTime();
for(j = 0; j < 1000; j++){
f.find(':visible').find("input,select,textarea,button").not("[type=hidden],[type=submit]").map(function() { return { order: i++, name: $(this).attr("name") }; } );
}
end = new Date().getTime();
return (end-start)/1000;
})();
// attempt 2: average about 0.221 seconds
(function(){
i = 0;
j = 0;
start = new Date().getTime();
for(j = 0; j < 1000; j++){
f.find("input:visible,select:visible,textarea:visible,button:visible").not("[type=hidden],[type=submit]").map(function() { return { order: i++, name: $(this).attr("name") }; } );
}
end = new Date().getTime();
return (end-start)/1000;
})();
// attempt 3: average about 0.261 seconds
(function(){
i = 0;
j = 0;
start = new Date().getTime();
for(j = 0; j < 1000; j++){
f.find("input[type!=hidden][type!=submit]:visible,select[type!=hidden][type!=submit]:visible,textarea[type!=hidden][type!=submit]:visible,button[type!=hidden][type!=submit]:visible").not("[type=hidden],[type=submit]").map(function() { return { order: i++, name: $(this).attr("name") }; } );
}
end = new Date().getTime();
return (end-start)/1000;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment