开发者

"chaining the calls to append() in your jquery" What is it, what is the risk of not doing it?

The comments section of this article: http://ayende.com/Blog/archive/2010/09/18开发者_StackOverflow社区/resolving-cross-site-scripting-issues.aspx has a very interesting comment.

In it, Mark mentions that I need to jain the calls to append to prevent "$(childDiv) lookup multiple times".

What is the impact of the vulnerability of "$(childDiv) lookup multiple times"?


No vulnerability really, it just slows it down.

$(childDiv).append(firstGroup).append(secondGroup);

is going to be faster than

$(childDiv).append(firstGroup);
$(childDiv).append(secondGroup);

Because it has to lookup childDiv twice.


Its the overhead of the lookup. Each $() is a function call. While $('#id') lookups are quick others are not so quick. In any event each time you do that you have to scour the entire DOM until you find the element(s) you want.

jquery functions return 'this' (a reference to themselves) so you can chain them with no extra lookup overhead.

another approach you can do is assign the jquery object to a local var

var mydiv = $('mydiv');

then do

mydiv.append();
mydiv.append();

If you are doing this thousands of times you will notice a big slowdown. (seconds of overhead on some browsers)


its a performance matter. Less look ups equals faster calculation. its also recommended that you try to use more ids than classes as they are much faster. and even with the ids is better to store them in variables and not to look ups again. where possible

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜