-1

What's the faster way to hide/show multiple elements, which are selected by .className?

$('.className1').show();
$('.className2').show();
$('.className3').show();

or

$('.className1, .className2, .className3').show();
doque
  • 2,723
  • 6
  • 27
  • 44

1 Answers1

0

The first option is faster.

The reason why is that a single class search is very performant in modern browsers (only slightly slower than an ID lookup). With multiple criteria, it has to find all elements first and then apply the selector logic.

The next issue will be "do you care"? If your code is called at human interaction speeds (e.g. in response to a mouse click) then you will never notice the difference. It only becomes apparent with lots of searches per second. Based on the JSPerf provided by @SatPal the difference is only 2-3%.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202