3

PHPStorm is telling me that

jQuery('#myform button[type="submit"]')

is a jQuery inefficient usage, and suggests that this is more efficient:

jQuery('#myform').find('button[type="submit"]')

Do you agree?

5
  • stackoverflow.com/a/16620060/783119
    – LazyOne
    Oct 3, 2015 at 20:28
  • 7
    Are you using this 100,000s of times in quick succession? If not then I really wouldn't worry about it. Readability should trump any micro optimizations IMHO.
    – Neilos
    Oct 3, 2015 at 20:29
  • 3
    This jsPerf test would indicate that the single selector is marginally quicker, however unless you're looping over 100,000+ elements you're not going to see any difference. And if that was the case, you have much bigger issues. Oct 3, 2015 at 20:31
  • It probably was quicker in 2010. The opposite is true now. Oct 3, 2015 at 20:31
  • 1
    @RoryMcCrossan Totally agree, the point would be that if you are worried about this then you either have amazing code that this is your biggest problem or you are worrying about the wrong things. And it's very unlikely that anyone's code is that good!
    – Neilos
    Oct 3, 2015 at 20:33

1 Answer 1

0

According to jsperf on Chrome (for this very specific example), looks like that's actually backwards.

http://jsperf.com/jquery-one-or-two

2
  • This test also has the same conclusion although the result is much tighter: jsperf.com/jquery-find-vs-context-2/47 Oct 3, 2015 at 20:29
  • I expect this may change significantly as you approach more "real-world" situations (deeper nesting, etc), and probably depends a lot on browser and version of jQuery. I expect unless you're running this a few thousand times repeatedly, it's probably totally irrelevant. Thanks for the insight Rory!
    – CollinD
    Oct 3, 2015 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.