I'm sitting over a snippet of code, which is superslow on mobile devices.
I have an entry form with about 40 input elements (buttons) in Jquery Mobile. In Jquery Mobile this will mean:
<div class="ui-btn" data-disabled="false">
<span class="ui-btn-inner">
<span class="ui-btn-text">Button</span>
</span>
<input type="button" value="Button" class="ui-btn-hidden" data-disabled="false">
</div>
Normally, I can just bind to the input
, which works ok on all PC browsers, but once I switch to mobile and the number of input elements grows, the buttons go dead.
As a workaround, I'm now binding to the closest ui-btn
, like this:
el.jqmData('bound', true )
.find('input')
.closest('.ui-btn')
.on('vclick', function(){
// do stuff
});
Which at least makes the element clickable, but still it's superslow.
I have been looking at jsPerf
examples comparing closest
to native Javascript methods (like here). If I assume a performance difference of 1:10 (like here when switching from desktop to mobile, I'm pretty sure my 'closest' calls are slowing things down.
My question:
Is there any way to replace closest
in my chained call with a native Javascript method? If so, how would this look like?
EDIT
I have pulled out the function in question. This is what I'm doing:
// this gets called once for the size entry box
// sizeEntryBox is the wrapper for $('sizeChart'), which contains the 60 inputs
addSizeHandlers = function( el ){
el.jqmData('boundStyle', true)
.find('.sizeChart')
.on('vclick', '.entry .ui-btn', function(){
// since I'm listening for .ui-btn I need to get the child input again
var qty = $( this ).find('input.qtyInput'),
// user can add +1/+2/+4
howMany = qty.closest('.ui-collapsible').find('.buttonBar input[name="radio_add"]:checked').val(),
qid = qty.attr('id').replace( /qty/, "" );
// a button may display a preset qty, clicking once replaces this with
// +1/+2/+4, clicking again adds +1/+2/+4
if ( qty.jqmData('flag') === false ){
qty.jqmData('flag',true)
newVal = parseFloat( howMany );
// set new value, add red borders
qty.val( howMany ).prev('.ui-btn-inner').addClass("brR").find('.ui-btn-text').html( howMany );
$('input[name=menge'+qid+']').val( howMany );
} else {
newVal = parseFloat( qty.val() ) + parseFloat( howMany );
qty.val( newVal ).prev('.ui-btn-inner').addClass("brR").find('.ui-btn-text').html( newVal );
$('input[name=menge'+qid+']').val( newVal );
}
});
},
So I'm already binding to the whole chart and delegating to the button. Still this takes forever on mobile devices, so some performance drainers jump to someones eye, please let me know.
Thanks again!