是“为了……在”吗? Javascript 中的循环总是不好?

发布于 2024-12-23 14:10:46 字数 878 浏览 3 评论 0原文

我遇到了一种情况,我正在尝试使用数组,但我认为它不会。我想要一个对象列表,每个对象都有一个唯一的 ID,并且我希望能够轻松引用特定对象,而不必循环遍历数组来搜索该 ID。

如果我使用对象,我可以轻松使用唯一 ID 作为键,将对象作为值。但是,如果我使用对象而不是数组,我将不得不使用 for...in 循环,而且我知道如果有人使用我的代码扩展了 ,就会出现问题对象.prototype

所以这是我的问题:

人们扩展Object.prototype真的很常见,以至于我应该厌倦使用它吗?还有其他原因导致我不想使用 for...in 循环吗?

另一方面,循环遍历数组寻找唯一 ID 的性能影响是否如此之小以至于我不应该担心?

(根据记录,该数组中可能包含相当少的元素,但我会非常频繁地访问它。此外,我正在编写的代码将是一个 jQuery 插件,所以我无法控制其他不良代码人们正在将其与它结合起来。)

更新:

根据 @nnnnnn 的建议,我设置了 jsperf 测试,结果如下: http://jsperf.com/accessing-object-properties-vs-iteating -over-arrays

基本上,虽然对象方式稍微快一些,但差异可以忽略不计。尽管如此,将 for...inhasOwnProperty 一起使用似乎更干净。

I have a situation that I'm trying to make work with an array, but I just don't think it will. I want to have a list of objects, each with a unique ID, and I want to be able to easily reference a particular object without having to loop through the array searching for that ID.

If I use an object I can easily use the unique ID as the key and the object as the value. However, if I use an object instead of an array I'll have to use a for...in loop, and I know there are issue surrounding that if someone using my code has extended Object.prototype.

So here's my question:

Is it really that common that people extend Object.prototype that I should be weary of using it? Are there other reasons why I wouldn't want to use a for...in loop?

On the other hand, is the performance hit of looping through an array looking for a unique ID so minimal that I shouldn't worry about?

(For the record, the array will probably have fairly few elements in it, but I'll be accessing it very frequently. Also, the code I'm writing will be a jQuery plugin, so I have no control over what other bad code people are combining it with.)

UPDATE:

Based on advice from @nnnnnn, I set up a jsperf test and here are the results:
http://jsperf.com/accessing-object-properties-vs-iterating-over-arrays

Basically, though the object way is slightly faster, the difference is negligible. Still, using for...in with hasOwnProperty seems cleaner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦巷 2024-12-30 14:10:46

即使有人扩展了原型,您也可以使用 hasOwnProperty属性来排除这些成员。 这正是 Sugar.js 推荐的

var s = 'cat', key;
for(key in s) {
  if(s.hasOwnProperty(key)) {
    console.log(key);
  }
}

现在根据 Sugar 的说法,for..in是对象文字的可接受循环,但您也可以使用迭代器,例如用于 jQuery 的 $.each 或用于 Underscore.js 的 _.each

Even if someone does extend the prototype, you can use hasOwnProperty property to exclude these members. This is exactly what Sugar.js recommends:

var s = 'cat', key;
for(key in s) {
  if(s.hasOwnProperty(key)) {
    console.log(key);
  }
}

Now according to Sugar, for..in is the accepted loop for object literals, but you can also use an iterator like $.each for jQuery or _.each for Underscore.js.

不美如何 2024-12-30 14:10:46

这是我的问题:

人们扩展 Object.prototype 真的很常见,以至于我应该厌倦使用它吗?还有其他原因导致我不想使用 for...in 循环吗?

不,这并不常见,但确实发生了。无论如何,您应该使用 for..in 循环,但使用 。 hasOwnProperty() 如其他答案所示。

我想不出任何理由使用 for..in (在对象上;显然你不在数组上使用它)。

另一方面,循环遍历数组寻找唯一 ID 的性能影响是否非常小,以至于我不应该担心?

好吧,您提到您的数组不会有很多元素,但我仍然认为即使它表现良好,它也会不必要地使您的代码复杂化,因为您根本不必对对象执行此操作。另一方面,使用 .hasOwnProperty() 是一个小复杂,它实际上成为每个 for..in 语法的一部分。

您可以在此处设置性能测试:http://jsperf.com/

So here's my question:

Is it really that common that people extend Object.prototype that I should be weary of using it? Are there other reasons why I wouldn't want to use a for...in loop?

No it's not common, but it does happen. You should use a for..in loop anyway, but with .hasOwnProperty() as shown in the other answers.

I can't think of any reasons to not use a for..in (on an object; obviously you don't use it on an array).

On the other hand, is the performance hit of looping through an array looking for a unique ID so minimal that I shouldn't worry about?

Well you mentioned that your array won't have many elements, but still I think that even if it performed well it would needlessly complicate your code given that you don't have to do it at all for objects. On the other hand using .hasOwnProperty() is a minor complication that effectively becomes part of the syntax of each for..in.

You can setup a test of the performance here: http://jsperf.com/

软的没边 2024-12-30 14:10:46

您可以使用 hasOwnProperty 方法在 for..in 循环中,以便将原型链上游的内容与分配给对象的内容区分开来。

或者您可以保留分配给给定对象的自己的“键”列表。

You could use the hasOwnProperty method in a for..in loop in order to distinguish stuff farther up the prototype chain from stuff assigned to your object.

Or you could keep your own list of "keys" assigned to a given object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文