[英]removeClass of Mootools is very slow on IE
I am applying addClass and removeClass on same set of elements in mooTools.addClass works fine but removeClass takes too long approx 6-7sec.Please see the code undeneath. 我在mooTools中的同一组元素上应用addClass和removeClass.addClass可以正常工作,但是removeClass花费的时间过长约6-7秒。请参见下面的代码。
$('usermailbox').getElements('input[class=emailmessages]').each(function(el){
el.getParents().addClass('unChecked');//works fine
});
$('usermailbox').getElements('input[class=emailmessages]').each(function(el){
el.getParents().removeClass('selected'); //Takes too long in IE
});
Do I have any hope ? 我有什么希望吗?
Right. 对。 Several issues here... Obviously, w/o seeing the DOM it is a little difficult to determine but:
这里有几个问题...显然,无法看到DOM很难确定,但是:
you do the same operations twice. 您执行两次相同的操作。 in the same instance you can addClass and removeClass
在同一实例中,您可以addClass和removeClass
doing element.getElements("input[class=emailmessages]")
vs element.getElements("input.emailmessages")
is probably slower but may return inputs that have other classes as well that you may not want. 与
element.getElements("input.emailmessages")
执行element.getElements("input[class=emailmessages]")
element.getElements("input.emailmessages")
较慢,但可能会返回包含您可能不想要的其他类的输入。
el.getParents() will return all parents. el.getParents()将返回所有父母。 you then iterate them again.
然后您再次迭代它们。 twice.
两次。 are you sure you don't mean just
.getParent()
, singular? 您确定不只意味着
.getParent()
吗? if so, or if its one parent only, you are applying a .each on a single element, which is an unnecessary hit. 如果是这样,或者仅是其一个父对象,则您正在单个元素上应用.each,这是不必要的选择。
if your logic needs to remain then consider this as a single iteration: 如果您的逻辑需要保留,则将其视为单个迭代:
store and walk. 存放和行走。
var parents = el.getParents();
parents.each(function(p) {
p.addClass("unchecked").removeClass("selected");
});
all in all: 总而言之:
$("usermail").getElements("input.emailmessages").each(function(el) {
var parents = el.getParents();
parents.each(function(p) {
p.addClass("unchecked").removeClass("selected");
});
});
of course, with Slick / mootools 1.3 you can do it a lot more simple: 当然,使用Slick / mootools 1.3,您可以轻松得多:
on this dom: 在这个dom上:
<div id="usermailbox">
<div class="selected">
<input class="emailmessages" />
</div>
<div class="selected">
<input class="emailmessages" />
</div>
<div class="selected">
<input class="emailmessages" />
</div>
</div>
the following will return all the divs: 以下将返回所有div:
// gets parents via a reverse combinator. you can do !> div or whatever to be more specific
// document.getElements("#usermailbox input.emailmessages !") will return all parents...
// ... in a single statement, inclusive of body, html etc... same as getParents
var divs = document.id("usermailbox").getElements("input.emailmessages !> ");
// single iteration per parent:
divs.each(function(div) {
div.removeClass("selected").addClass("unChecked");
});
of course, you can just do $("useremail").getElements("div.selected,div.unChecked")
to get to these divs at any time anyway.... so it all depends on your DOM and needs, there must be a reason why you are doing what you are doing. 当然,您可以随时执行
$("useremail").getElements("div.selected,div.unChecked")
来获取这些div。...所以这完全取决于您的DOM和需求,在那里一定是您正在做自己的事情的原因。
bottom line for perf: 性能的底线:
cache results of lookups into vars. 将查找结果缓存到var中。 if you call
$("someid")
twice, cache it in your scope. 如果您两次调用
$("someid")
,则将其缓存在您的范围内。 if you do $("someid").getElements()
twice, that's more than twice as bad in performance... and add .getParents()
twice, thats ... n-times as bad now... 如果您执行
$("someid").getElements()
两次,那么性能就会提高两倍以上...并添加.getParents()
两次,那就是...现在的n倍...
avoid applying chaqined methods to collections like this: collection.addClass("foo").removeClass("bar")
- it will iterate it twice or n-times, go for a single .each
callback instead, it will be much faster. 避免将混乱的方法应用于这样的集合:
collection.addClass("foo").removeClass("bar")
-它将迭代两次或n次,执行单个.each
回调,它将更快。
try to avoid reverse combinators or parents lookups if possible, go direct. 尽量避免反向组合器或父母查找,请直接进行。 you can use
nth-child
etc - other ways to walk your DOM than to get to the input and walk back. 您可以使用
nth-child
等-遍历DOM的其他方式,而不是获取输入并向后追溯。 Especially when you don't really need the input itself... 特别是当您真的不需要输入本身时...
.getParents(selector)
will limit the types of parents you want. .getParents(selector)
将限制您想要的父母的类型。 .getParents()
will return buckloads, all the way up the parent / anchor dom node, often including the same ones for siblings. .getParents()
将一直返回父节点/锚点dom节点的负载,对于兄弟姐妹通常包括相同的负载。
always create a local scope with anonymous functions so you don't pollute your global object or upper scopes - IE does not like that and makes accessing variables slow. 始终使用匿名函数创建本地作用域,因此您不会污染全局对象或上层作用域-IE不喜欢那样,并且会使访问变量变慢。
Hope some of this makes sense, at least :) good luck and remember, speed is only relative and is as good as your performance in the slowest browser you support. 希望其中至少有一部分是有意义的:)祝您好运,请记住,速度只是相对的,与您所支持的最慢浏览器的性能一样好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.