“in”中Big O表示法的效率是多少? operator或obj.hasOwnProperty(prop)

编程入门 行业动态 更新时间:2024-10-23 01:34:11
本文介绍了“in”中Big O表示法的效率是多少? operator或obj.hasOwnProperty(prop)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Mozilla的网站清楚地描述了 hasOwnProperty() 和 运营商。

Mozilla's website clearly describes hasOwnProperty() and the in operator.

但是,它没有提供有关其效率的任何实施细节。

However, it does not give any implementation details in regards to their efficiencies.

我怀疑他们是 O(1)(常数时间)但是很想看到任何可能存在的引用或测试。

I would suspect they'd be O(1) (constant time) but would love to see any references or tests that might exist.

推荐答案

将我的评论转化为答案。

To turn my comments into an answer.

hasOwnProperty() 应该 O(1),因为它是一个键查找,但是它将是特定于实现的。

hasOwnProperty() should be O(1), as it is a key lookup, but it will be implementation specific.

在中肯定会更复杂(尽管应该与 hasOwnProperty()如果该对象上存在该属性,则为g oes the the prototype chain,寻找那个属性。这就是为什么经常建议使用 hasOwnProperty()进行迭代,使用对(in)进行迭代。

in will most certainly be more complicated (though should be the same as hasOwnProperty() if the property exists on that object), as it goes up the prototype chain, looking for that property. That is why it is often recommended to use hasOwnProperty() when iterating over object properties with for ( in ).

要查明,请检查这些功能的源代码。使用来源,卢克:)

To find out, inspect the source code of these functions. Use the source, Luke :)

更多推荐

“in”中Big O表示法的效率是多少? operator或obj.hasOwnProperty(prop)

本文发布于:2023-11-30 00:47:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:效率   operator   Big   prop   hasOwnProperty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!