在Javascript中,最常见的计算能力浪费是什么?

时间:2021-11-30 15:49:18

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.

等等。我想知道人们看到了什么最常见的JavaScript/jQuery技术在减慢页面速度和/或浪费JavaScript引擎的时间。

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?"

我知道这个问题似乎不符合什么是可接受的问题,但我在问“最常见的可接受的浪费是什么?”

5 个解决方案

#1


3  

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.

我内疚的。在jQuery选择器中只使用元素的类。而不是将类选择器与元素标记名组合在一起。

<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 ] )

$(选择器,[context])


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

Which can be handled better like this:

可以这样处理:

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

#2


1  

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.

这似乎是显而易见的,但你会一直看到它。

#3


1  

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.

我猜在非常复杂的html中错误地使用类而不是id作为选择器会使事情变慢。当然和ie。

#4


0  

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

调用美元。动画的元素应该使事物慢下来。

#5


0  

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().

不从getgo声明vars以便缓存它们,不使用闭包和重复相同函数/调用/等的x号,而是使用eval()对每个vars进行id或类的更改。

#1


3  

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.

我内疚的。在jQuery选择器中只使用元素的类。而不是将类选择器与元素标记名组合在一起。

<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 ] )

$(选择器,[context])


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

Which can be handled better like this:

可以这样处理:

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

#2


1  

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.

这似乎是显而易见的,但你会一直看到它。

#3


1  

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.

我猜在非常复杂的html中错误地使用类而不是id作为选择器会使事情变慢。当然和ie。

#4


0  

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

调用美元。动画的元素应该使事物慢下来。

#5


0  

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().

不从getgo声明vars以便缓存它们,不使用闭包和重复相同函数/调用/等的x号,而是使用eval()对每个vars进行id或类的更改。