My fellow programmers, i'am losing my mind. I've reed many books on JS performance and all of them were saying, that it is better to cache DOM objects for future references and management and not creating new Jquery objects. It sounds reasonable. But now my faith is shaking. Im trying to develop Code conventions in my company and i was goin to adress this problem from a performance stand point. I've got piece of code that follows:
$("#registratorType").attr("readonly", "readonly");
$("#registratorType").attr("size", "25");
$("#id").attr("readonly", "readonly");
$("#id").attr("size", "25");
$("#serial").attr("readonly", "readonly");
$("#serial").attr("size", "25");
$("#duration").attr("size", "25");
$("#stoppingDuration").attr("size", "25");
$("#speeddata").removeAttr("disabled");
Its not the whole thing but i think, that you've got the idea. I was going to get all this form and wrap it in the cached reference.
var $form = $('#vehicleProfile');
$form.find("#registratorType").attr("readonly", "readonly");
$form.find("#registratorType").attr("size", "25");
$form.find("#id").attr("readonly", "readonly");
$form.find("#id").attr("size", "25");
$form.find("#serial").attr("readonly", "readonly");
$form.find("#serial").attr("size", "25");
$form.find("#duration").attr("size", "25");
$form.find("#stoppingDuration").attr("size", "25");
$form.find("#speeddata").removeAttr("disabled");
I've tested performance in the latest chrome and was shocked! My approach was losing in 3-4 times. I mean my function runned at 42 ms, while old one was 12-14ms. Is there REALLY any value to my method? Is my approach an anti-pattern? Help me please!