variables - jQuery - Is it okay to use $('#ElementId') everytime? -
i learned how 'document.getelementbyid' counterpart in jquery (and it's more powerful). question is, okay use everytime or every line of code? here's how use now:
$('#myparentelement').html('<tr id="' + $('#myelement').val() + '"><td>' + $('#myelement').val() + '</td></tr>';
isn't better if using variable reference object?
var x = $('#myelement'); $('#myparentelement').html('<tr id="' + x.text() + '"><td>' + x.text() + '</td></tr>';
note i'm more concern of performance, not cleanliness of codes.
dom selection expensive. cache it.
var x = $('#myelement');
here's jsperf test. in chrome 13 on mac os x, variable reference on 1,000 times faster.
this not due dom selection of course, construction of jquery object.
Comments
Post a Comment