迭代性能比较

发布于 2024-12-05 15:39:58 字数 324 浏览 0 评论 0 原文

我有一个数组:

deleteIds= ["User1"];

并尝试对其进行迭代,如下所示:

第一个:

for (var index = 0; index < len; index++) {
    alert(deleteIds[index]);
}

第二个:

for (var index in deleteIds) {
    alert(deleteIds[index]);
}

它们的性能比较是什么?

I have an array:

deleteIds= ["User1"];

and try to iterate over it as like:

first one:

for (var index = 0; index < len; index++) {
    alert(deleteIds[index]);
}

second one:

for (var index in deleteIds) {
    alert(deleteIds[index]);
}

What is the performance comparison of them?

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

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

发布评论

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

评论(1

我早已燃尽 2024-12-12 15:39:58

这充其量只是一种微观优化。除非您的 deleteIds 数组中有数十万个元素,否则您不应该考虑对此进行优化。

事实证明,for(;;;) 版本比 for in 更快:http://jsperf.com/iteating-over-a-loop/2 (感谢 @Jamiec 的 jsperf)。

然而,比这些代码的性能比较更重要的是,两个代码片段在功能上彼此不同(实例)。

var array = new Array(100);
for (var i = 0; i < array.length; i++) {
    console.log(i); // shows 0 - 99
}

for (var x in array) {
    console.log(x); // shows nothing
}

此外,如果您向数组或数组原型链中的对象添加方法,它们将显示在 for (var x in y) 循环中,但不会显示在 中for (;;;);现场示例):

Array.prototype.foo = function() {};
var array = [];
for (var i = 0; i < array.length; i++) {
    console.log(i); // nothing
}

for (var x in array) {
    console.log(x); // `foo`
}

您可以使用 hasOwnProperty< /code> 消除继承的属性来自原型链,但这不会阻止您直接在对象上接收方法(实时示例) :

Array.prototype.foo = function() {};
var array = [];
array.bar = function () {};

for (var i = 0; i < array.length; i++) {
    console.log(i); // nothing
}

for (var x in array) {
    if (array.hasOwnProperty(x)) {
        console.log(x); // `bar`
    }
}

正是由于这些原因,不鼓励使用 for in 来迭代数组,并且 for(;;;) 版本应该始终 代替使用。 for in 应用于迭代对象

This is a micro optimisation at best. Unless you have hundreds of thousands of elements in your deleteIds array, you should not be looking at optimising this.

It turns out that the for(;;;) version is quicker than for in: http://jsperf.com/iterating-over-a-loop/2 (thanks to @Jamiec for the jsperf).

However, what's more important than the performance comparison of these, is that both code snippets are functionally different to each other (live example).

var array = new Array(100);
for (var i = 0; i < array.length; i++) {
    console.log(i); // shows 0 - 99
}

for (var x in array) {
    console.log(x); // shows nothing
}

Furthermore, if you add methods to either the array, or to an object in the arrays' prototype chain, they will show up in the for (var x in y) loop, but not in for (;;;); (live example):

Array.prototype.foo = function() {};
var array = [];
for (var i = 0; i < array.length; i++) {
    console.log(i); // nothing
}

for (var x in array) {
    console.log(x); // `foo`
}

You can use hasOwnProperty to eliminate the attributes inherited from the prototype chain, but that won't stop you receiving methods directly on the object (live example):

Array.prototype.foo = function() {};
var array = [];
array.bar = function () {};

for (var i = 0; i < array.length; i++) {
    console.log(i); // nothing
}

for (var x in array) {
    if (array.hasOwnProperty(x)) {
        console.log(x); // `bar`
    }
}

It is because of these reasons that using for in for iterating over an array is discouraged, and the for(;;;) version should always be used instead. for in should be used for iterating over an object.

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