开发者

JQUERY - Speeding Up Code

I have a generic question. I've heard many times that a great way to speed up your code is to not call the same selector over and over again - eg - 4 times one DIV selector:

    if($('div#spotJoinSite').is(':visible')) {
        var pos = $('div#spotJoinSite').offset();  
        var width = $('div#spotJoinSite').width();
        var height = $('div#spotJoinSite').height();
        $('div#container_join_messages').css({'position': 'fixed'});        
    }

Would it therefore be a good idea to have a section at the top of a js script that connected all the required selectors to variables - eg:

        var spotJoinSite = $('div#spotJoinSite');
        var container_join_messages = $('div#container_join_messages');

The the variables could be exclusively used.

Is this a good idea or a bad idea? What speed increases could be expected? noticable or not? What are the potential problems?开发者_Go百科 Would you do it?

If the variable names are same as the selector it would be easy to reference them.


Yes, it is proabably noticeable on slow computers or on very hard applications like minigames in jQuery/javascript

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜