简体   繁体   English

Vanilla Javascript 和 Dom 性能

[英]Vanilla Javascript & Dom Performance

I've been learning Javascript online and have recently started to put it to use on a website.我一直在网上学习 Javascript,最近开始在网站上使用它。 I'm still a little confused about what might be the most efficient/performant ways to do things, though.不过,我仍然对什么可能是最有效/最高效的做事方式感到困惑。 A few questions with that in mind...考虑到这一点,有几个问题......

  1. If a webpage has multiple click events in different sections, each doing something different, one event listener in the body tag — with multiple if/else statements — would be the most efficient way to handle them all.如果一个网页在不同的部分有多个点击事件,每个都做不同的事情,body 标签中的一个事件侦听器——带有多个 if/else 语句——将是处理它们的最有效方法。 Is that correct?那是对的吗?

  2. Is the search method a good way to handle if/else statements in this case?在这种情况下,搜索方法是处理 if/else 语句的好方法吗? For example:例如:

     if (event.target.className.search("js-tab") !== -1) { // do something } else if (event.target.className.search("js-dropdown") !== -1) { // do something else }
  3. The ID performance only applies to actually finding an element, right? ID 性能只适用于实际查找元素,对吗? There wouldn't be a difference between event.target.className.search and event.target.id.search, would there? event.target.className.search 和 event.target.id.search 之间不会有区别,是吗? (assuming there aren't an insane amount of class names to search through on that element). (假设在该元素上搜索的类名数量不多)。 I'm currently using className for sections that have the same functionality (multiple tabbed sections on the same page, for example).我目前正在将 className 用于具有相同功能的部分(例如,同一页面上的多个选项卡式部分)。 I suppose I could just as easily use the or operator if it made a difference, like so:我想我可以很容易地使用 or 运算符,如果它有所作为,就像这样:

     if ((event.target.id.search("js-tab-one") !== -1) || (event.target.id.search("js-tab-two") !== -1))
  4. When there are multiple elements that a click event could potentially be on (an icon inside of an anchor link, for example), how much of a performance hit is it to add additional if/else statements (ie check the tag type, and if it's not the a tag, move up)?当点击事件可能存在多个元素时(例如,锚链接内的图标),添加额外的 if/else 语句(即检查标签类型,如果这不是 a 标签,向上移动)? I recently refactored my css so that my icons were set to width: 100% and height: 100% (ensuring that the click was always happening on the same element every time), but I wonder how much of a performance boost (if any at all) I actually got by doing this?我最近重构了我的 css,以便我的图标设置为宽度:100% 和高度:100%(确保每次点击总是发生在同一个元素上),但我想知道有多少性能提升(如果有的话) all)我实际上是通过这样做得到的?

A lot of what you're asking is subjective though and it fits in the context that is trying to happen.不过,您所问的很多问题都是主观的,并且符合正在尝试发生的上下文。 So there might be a reason why the below might be not good (as another commenter said, don't prematurely optimize).因此,以下内容可能不好可能是有原因的(正如另一位评论者所说,不要过早优化)。 But there are some programming techniques that can be said about what you have done, namely modularity.但是对于您所做的事情,可以说一些编程技术,即模块化。 There's a few performance considerations but they mostly revolve around writing good code:有一些性能方面的考虑,但它们主要围绕编写好的代码:

1) it really depends on your handler, if you intend to delete and add DOM elements, this can be better because you watch the document instead of tying in a new click handler to each element. 1) 这真的取决于您的处理程序,如果您打算删除和添加 DOM 元素,这可能会更好,因为您观看文档而不是将新的点击处理程序绑定到每个元素。 but this can be bad because its not modular if you have it do too much stuff.但这可能很糟糕,因为如果你让它做太多事情,它就不是模块化的。 But if its just looking at the state, then this is not really good performance wise....但如果它只是看状态,那么这在性能方面并不是很好......

2) i wouldn't say that the search has any impact on the if else's, but in considering optimization of this particular situation you describe later on, I wouldn't do that. 2)我不会说搜索对 if else 有任何影响,但是在考虑优化您稍后描述的这种特定情况时,我不会这样做。 As each time you go through each if-else you end up doing another search and it can add up in the long run if you have a lot of if-elses in the way.每次遍历每个 if-else 时,您最终都会进行另一次搜索,如果有很多 if-else 的话,从长远来看它会加起来。

3) there shouldn't be a difference but why search in an id string? 3) 应该没有区别,但为什么要在 id 字符串中搜索? unless your id string is part of some unholy long part like musical-js-tab-one-musical-tab which I wouldn't suggest to use as an id like this anyway, you should split it up, you shouldn't tie functionality like that, use classes instead like class='musical' id='js-tab-one' since id's should describe a singular object on the page.除非您的 id 字符串是像 music-js-tab-one-musical-tab 这样的邪恶长部分的一部分,我不建议将其用作这样的 id,否则您应该将其拆分,您不应该绑定功能像那样,使用类代替 class='musical' id='js-tab-one' 因为 id 应该描述页面上的单个对象。

4) everytime you add an if-else, no matter how small it is, is another computer cycle and a little bit more memory. 4)每次加一个if-else,不管多小,都是一个电脑周期,多一点内存。 and it can add up, just attach your click handler by tag and by class at this point and let the browser decide to optimize it.它可以累加,此时只需按标签和类附加您的点击处理程序,然后让浏览器决定对其进行优化。

If a webpage has multiple click events in different sections, each doing something different, one event listener in the body tag — with multiple if/else statements — would be the most efficient way to handle them all.如果一个网页在不同的部分有多个点击事件,每个都做不同的事情,body 标签中的一个事件侦听器——带有多个 if/else 语句——将是处理它们的最有效方法。 Is that correct?那是对的吗?

No. It's useful for handling events on elements that you add dynamically, but not so much for regular events.不。它对于处理动态添加的元素上的事件很有用,但对于常规事件则不然。

Handling all the events in the body means that you will handle all the events.处理正文中的所有事件意味着您将处理所有事件。 Every click that happens goes through your code to see if it comes from one of the elements that you are interested in.发生的每一次点击都会遍历您的代码,以查看它是否来自您感兴趣的元素之一。

Is the search method a good way to handle if/else statements in this case?在这种情况下,搜索方法是处理 if/else 语句的好方法吗?

No. The search method uses a regular expression, not a string, so the string will be parsed to create a RegExp object.不可以。 search方法使用正则表达式,而不是字符串,因此将解析字符串以创建 RegExp 对象。 Then a search is done using the regular expression, which is slower than a normal string search, and can give unexpected results.然后使用正则表达式完成搜索,这比正常的字符串搜索慢,并且可能会给出意想不到的结果。

To look for a string in another string you should use the indexOf method instead.要在另一个字符串中查找字符串,您应该改用indexOf方法。 You should however be aware that it will look for a match anywhere in the string, not matching whole class names.但是你应该知道它会在字符串中的任何地方寻找匹配,而不是匹配整个类名。 It will for example find "all-head" when the element has the class "small-header" .例如,当元素具有类"small-header"时,它将找到"all-head" "small-header"

The ID performance only applies to actually finding an element, right? ID 性能只适用于实际查找元素,对吗?

Yes.是的。

When there are multiple elements that a click event could potentially be on (an icon inside of an anchor link, for example), how much of a performance hit is it to add additional if/else statements (ie check the tag type, and if it's not the a tag, move up)?当点击事件可能存在多个元素时(例如,锚链接内的图标),添加额外的 if/else 语句(即检查标签类型,如果这不是 a 标签,向上移动)?

That means that every click event will have to go through the extra if statements.这意味着每个点击事件都必须经过额外的if语句。

If you bind the event to the element instead, you know that the event happened inside that element and you don't have to know which element did actually catch it.如果您改为将事件绑定到元素,您就会知道该事件发生在该元素内部,而您不必知道哪个元素确实捕获了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM