JavaScript 开发人员是否有理由不使用 Array.push()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15649899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:32:20  来源:igfitidea点击:

Is there a reason JavaScript developers don't use Array.push()?

javascript

提问by bloudermilk

I commonly see developers use an expression like the following in JavaScript:

我经常看到开发人员在 JavaScript 中使用如下表达式:

arr = []
arr[arr.length] = "Something"
arr[arr.length] = "Another thing"

Wouldn't pushbe more appropriate?

不是push更合适吗?

arr = []
arr.push("Something")
arr.push("Another thing")

回答by gkiely

I actually asked myself the same question at the start of this year. UPDATED with new test caseshttp://jsperf.com/array-push-vs-unshift-vs-direct-assignment/2

事实上,我在今年年初问过自己同样的问题。更新了新的测试用例http://jsperf.com/array-push-vs-unshift-vs-direct-assignment/2

It appears that pushis much faster in chrome, and about equal in FF. Also direct is faster in IE9, but I would be interested to see how it performs in IE10.

看起来这push在 chrome 中要快得多,在 FF 中大约相等。直接在 IE9 中也更快,但我很想看看它在 IE10 中的表现如何。

I would say that most developers would assume setting the length of the array, and then using direct assignment is faster, as is the case with most programming languages. But JavaScript is different. Javascript arrays aren't really arrays, they're just key/value maps just like all other JavaScript objects. So the pre-allocation is essentially falling on deaf ears.

我会说大多数开发人员会假设设置数组的长度,然后使用直接赋值会更快,就像大多数编程语言一样。但 JavaScript 不同。Javascript 数组并不是真正的数组,它们只是键/值映射,就像所有其他 JavaScript 对象一样。所以预分配基本上是置若罔闻。

Personally I prefer push(:

我个人更喜欢push(:

回答by Guffa

I believe that it's mostly habit.

我相信这主要是习惯。

Some developers use it simply because it's the way they are used to do it, and haven't considered that pushwould be an alternative.

一些开发人员使用它只是因为这是他们习惯的方式,并且没有考虑过push这是替代方案。

Some developers have learned once upon a time that one method is much faster than another, and haven't reviewed this in light of the recent performance improvements of the Javascript engines.

一些开发人员曾一度了解到,一种方法比另一种方法快得多,但尚未根据 Javascript 引擎最近的性能改进对其进行。

Personally I use pushfrequently. Most of the time readability and maintainability is more important than performance, at least when the performance impact is small enough. The performance tests posted in the answers here show that the performance difference between various methods isn't very big.

我个人push经常使用。大多数时候可读性和可维护性比性能更重要,至少在性能影响足够小时。此处的答案中发布的性能测试表明,各种方法之间的性能差异并不是很大。

回答by gaby de wilde

It's a way to limit nested braclets. If you have enough of them you cant see howmany there are or howmany you need (when later looking at the code). I would use a var, one should only have to count things one time.

这是一种限制嵌套小括号的方法。如果你有足够的它们,你就看不到有多少或你需要多少(稍后查看代码时)。我会使用 var,一个人应该只需要计算一次。

bar = foo.length;
foo[ bar++ ] = "new item 0";
foo[ bar++ ] = "new item 1";
foo[ bar++ ] = "new item 2";
foo[ bar++ ] = "new item 3";

http://jsfiddle.net/vEUU3/

http://jsfiddle.net/vEUU3/

回答by jbodily

Very often when I'm pushing an object into an array, I want the reference to that object to be returned to me. For example:

很多时候,当我将一个对象推入一个数组时,我希望将该对象的引用返回给我。例如:

// Returns object
function createAndAdd(arr) {
  return arr[arr.length] = { id: 1 };
} 
var obj = createAndAdd(arr);

Now I have the new object andI've added it to an array. If I'd used push then I would have been returned the length of the array. It would look like the following:

现在我有了新对象并将它添加到数组中。如果我使用了 push 那么我会返回数组的长度。它看起来像下面这样:

function createAndAdd(arr) {
  var obj = { id: 1 };
  arr.push(obj);
  return obj;
}

or even uglier, especially if the object gets big:

甚至更丑,尤其是当物体变大时:

function createAndAdd(arr) {
  return arr[arr.push({ id: 1 }) -1];
}

Personally, I love functions with immediate return statements. Keeps it simple and easy to look at. Love the question. Thanks for asking it.

就个人而言,我喜欢带有立即返回语句的函数。保持简单易看。喜欢这个问题。谢谢你问它。

回答by YNoobDemon

Array.prototype.push method returns the updated array's length while the direct declaration returns the value that is being assigned. That's the only difference I see, but I usually read some best-practices recommendations about the push method being a better way to assign new values to an array.

Array.prototype.push 方法返回更新后的数组的长度,而直接声明返回正在分配的值。这是我看到的唯一区别,但我通常会阅读一些关于 push 方法是将新值分配给数组的更好方法的最佳实践建议。

回答by Daniel Maldonado

I think its not about performance, or at least is not such a big deal... what really matters here is declarativeway vs imperativeway.

我认为这与性能无关,或者至少不是什么大问题……这里真正重要的是声明式方式与命令式方式。