Is ID selector faster than class selector when it is cached in jquery

后端 未结 5 1344
长发绾君心
长发绾君心 2020-12-21 05:01

I know that ID is a faster selector than class in Javascript. But what if I cache the selector? When the selector is cached, would it differ in speed if it’s a class selecto

5条回答
  •  难免孤独
    2020-12-21 05:47

    Just to make sure I'm not missing the mark... I think you mean

    and then in jquery your doing:

    var $myclass = $('.myclass');
    var $myid = $('#myid');
    

    My understanding of jquery is that when creating the vars creating the $myclass is not as fast as creating the $myid... but when you go back to use them later. they will be the same speed.

提交回复
热议问题