开发者

What is the most common waste of computing power in Javascript?

We've all seen people who do this:

jQuery('a').each(function(){
    jQuery(this)[0].innerHTML += ' proccessed';
});

function letsPoluteNS() {
    polute = '';
    for (morePolution = 0; morePolution < arguments.length; morePolution++)
        polute.join(arguments[morePolution]);
 开发者_开发知识库   return polute;
}

and so on. I was wondering what people have seen the most common JavaScript/jQuery technique that is slowing down the page and/or wasting time for the JavaScript engine.

I know that this question may not seem to fit into what's an accepted question, yet I'm asking "what is the most common accepted waste?"


I'm guilt of this. Basically using only the element's class in a jQuery selector. Instead of combining the class selector with the elements tag name.

<div></div>
<div class="hide"></div>
<div class="show"></div>
<div class="hide"></div>
<div class="hide again"></div>

$(".hide").hide();

Instead of the quicker

$("div.hide").hide()


Also this is inefficient, many people don't make use of the context parameter for selectors

$( selector, [ context ] )


   $("#mydiv").click(function () {
      $("#mydiv span").show();
   }

Which can be handled better like this:

   $("#mydiv").click(function () {
      $("span", this).show();
   }


You'll also see this:

$('#this').find('a').doSomeThing();

Know what's a lot more efficient? One selector that covers both will server you better...

$('#this a').doSomeThing();

It seems obvious, but you'll see it all the time.


Anything that has do to with tracking users and heavy publicity. Thats wasted space for sure.

I guess wrong use of stuff like using classes instead ids as selector in very complex html would slow thing down. And ie of course.


Calling $.animate to animate elements should make the things slow down.


not declaring your vars from the getgo so they are cached, not using closures and repeating x number of the same function/call/etc but only changing id or class for each, using eval().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜