javascript 添加到数组时使用 push 方法或 .length 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6772757/
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
Using the push method or .length when adding to array?
提问by mowwwalker
What are the downsides to doing:
这样做的缺点是什么:
var myArray = [];
myArray[myArray.length] = val1;
myArray[myArray.length] = val2;
instead of:
代替:
var myArray = [];
myArray.push(val1);
myArray.push(val2);
I'm sure the push method is much more "acceptable", but are there any differences in functionality?
我确定 push 方法更“可接受”,但在功能上有什么区别吗?
采纳答案by AlienWebguy
push is way faster, almost 300% faster.
push 速度更快,几乎快 300%。
回答by ThiefMaster
Since arrays in JavaScript do not have holes the functionality of those two methods is equal. And yes, using .push()
is much cleaner (and shorter).
由于 JavaScript 中的数组没有漏洞,因此这两种方法的功能是相同的。是的,使用.push()
更干净(更短)。
回答by andyb
I've generally thought lengthassignment was faster. Just found Index vs. push performancewhich backs that up; for my Chrome 14 browser anyway, over a single test run. However there is not much in it in Chrome.
我通常认为长度分配更快。刚刚发现Index vs. push 性能支持这一点;无论如何,对于我的 Chrome 14 浏览器,通过一次测试运行。但是,Chrome 中的内容并不多。
回答by kzh
There seems to be discrepancy on which test is faster among the varying JavaScript engines. The differences in speed may be negligible (unless an unholy amount of pushes are needed). In that case, the prudent developer should always err on the side of readability. In this case, in my opinion and the opinion of @TheifMasteris that [].push()
is cleaner and it is easier to read. Maintenance of code is the most expensive part of coding.
在不同的 JavaScript 引擎中,哪种测试更快,似乎存在差异。速度的差异可以忽略不计(除非需要大量的推动)。在这种情况下,谨慎的开发人员应该始终在可读性方面犯错。在这种情况下,在我看来,并认为@TheifMaster是[].push()
更清洁和更容易阅读。代码维护是编码中最昂贵的部分。
回答by Howard
As I tested, the first way is faster, I'm not sure why, keep researching. Also the ECMA doesn't mentioned which one is better, I think it is depending on how the browser vendor implements this.
正如我测试的那样,第一种方法更快,我不知道为什么,继续研究。ECMA 也没有提到哪个更好,我认为这取决于浏览器供应商如何实现这一点。
var b = new Array();
var bd1 = new Date().getTime();
for(var i =0;i<1000000; i++){
b[b.length] = i;
};
alert(new Date().getTime()- bd1);
var a = new Array();
var ad1 = new Date().getTime();
for(var i =0;i<1000000; i++){
a.push(i);
};
alert(new Date().getTime()- ad1);
回答by Redu
In JS there are 3 different ways you can add an element to the end of an array. All three have their different use cases.
在 JS 中,有 3 种不同的方式可以将元素添加到数组的末尾。这三个都有不同的用例。
1) a.push(v), a.push(v1,v2,v3), a.push(...[1,2,3,4]), a.push(..."test")
1) a.push(v), a.push(v1,v2,v3), a.push(...[1,2,3,4]), a.push(..."test")
Push is not a very well thought function in JS. It returns the length of the resulting array. How silly. So you can never chain push() in functional programming unless you want to return the length at the very end. It should have returned a reference to the object it's called upon. I mean then it would still be possible to get the length if needed like a.push(..."idiot").length
. Forget about push if you have intentions to do something functional.
Push 在 JS 中并不是一个经过深思熟虑的功能。它返回结果数组的长度。多么愚蠢。所以你永远不能在函数式编程中链接 push() ,除非你想在最后返回长度。它应该返回对它调用的对象的引用。我的意思是,如果需要,仍然可以获取长度,例如a.push(..."idiot").length
. 如果您打算做一些功能性的事情,请忘记推送。
2) a[a.length] = "something"
2) a[a.length] = "某物"
This is the biggest rival of a.push("something"). People fight over this. To me the only two differences are that
这是 a.push("something") 最大的竞争对手。人们为此而斗争。对我来说,唯一的两个区别是
- This one returns the value added to the end of the array
- Only accepts single value. It's not as clever as push.
- 这个返回添加到数组末尾的值
- 只接受单个值。它不像推动那么聪明。
You shall use it if the returned value is of use to you.
如果返回的值对您有用,您应该使用它。
3. a.concat(v), a.concat(v1,v2,v3), a.concat(...[1,2,3,4]), a.concat([1,2,3,4])
3. a.concat(v), a.concat(v1,v2,v3), a.concat(...[1,2,3,4]), a.concat([1,2,3,4] ])
Concat is unbelievably handy. You can use it exactly like push. If you pass the arguments in array it will spread them to the end of the array it's called upon. If you pass them as separate arguments it will still do the same like a = a.concat([1,2,3],4,5,6); //returns [1, 2, 3, 4, 5, 6]
However don't do this.. not so reliable. It's better to pass all arguments in an array literal.
Concat 非常方便。你可以像推一样使用它。如果您在数组中传递参数,它会将它们传播到它所调用的数组的末尾。如果你将它们作为单独的参数传递,它仍然会做同样的a = a.concat([1,2,3],4,5,6); //returns [1, 2, 3, 4, 5, 6]
事情但是不要这样做..不那么可靠。最好在数组文字中传递所有参数。
Best thing with concat is it will return a reference to the resulting array. So it's perfect to use it in functional programming and chaining.
concat 最好的事情是它将返回对结果数组的引用。所以在函数式编程和链接中使用它是完美的。
Array.prototype.concat()
is my preference.
Array.prototype.concat()
是我的偏好。
4) A new push() proposal
4) 一个新的 push() 提议
Actually one other thing you can do is to overwrite the Array.prototype.push()
function like;
实际上,您可以做的另一件事是覆盖Array.prototype.push()
函数,例如;
Array.prototype.push = function(...args) {
return args.reduce(function(p,c) {
p[p.length] = c;
return p
}, this)
};
so that it perfectly returns a reference to the array it's called upon.
以便它完美地返回对其调用的数组的引用。