在JavaScript中创建任意长度的零填充数组最有效的方法是什么?
当前回答
我知道我在某个地方有这个原型:)
Array.prototype.init = function(x,n)
{
if(typeof(n)=='undefined') { n = this.length; }
while (n--) { this[n] = x; }
return this;
}
var a = (new Array(5)).init(0);
var b = [].init(0,4);
编辑:测试
作为对Joshua和其他方法的回应,我运行了自己的基准测试,我看到了与报告完全不同的结果。
以下是我测试的内容:
//my original method
Array.prototype.init = function(x,n)
{
if(typeof(n)=='undefined') { n = this.length; }
while (n--) { this[n] = x; }
return this;
}
//now using push which I had previously thought to be slower than direct assignment
Array.prototype.init2 = function(x,n)
{
if(typeof(n)=='undefined') { n = this.length; }
while (n--) { this.push(x); }
return this;
}
//joshua's method
function newFilledArray(len, val) {
var a = [];
while(len--){
a.push(val);
}
return a;
}
//test m1 and m2 with short arrays many times 10K * 10
var a = new Date();
for(var i=0; i<10000; i++)
{
var t1 = [].init(0,10);
}
var A = new Date();
var b = new Date();
for(var i=0; i<10000; i++)
{
var t2 = [].init2(0,10);
}
var B = new Date();
//test m1 and m2 with long array created once 100K
var c = new Date();
var t3 = [].init(0,100000);
var C = new Date();
var d = new Date();
var t4 = [].init2(0,100000);
var D = new Date();
//test m3 with short array many times 10K * 10
var e = new Date();
for(var i=0; i<10000; i++)
{
var t5 = newFilledArray(10,0);
}
var E = new Date();
//test m3 with long array created once 100K
var f = new Date();
var t6 = newFilledArray(100000, 0)
var F = new Date();
结果:
IE7 deltas:
dA=156
dB=359
dC=125
dD=375
dE=468
dF=412
FF3.5 deltas:
dA=6
dB=13
dC=63
dD=8
dE=12
dF=8
因此,据我估计,总体而言,推送速度确实较慢,但在FF中使用较长的阵列时表现更好,但在IE中表现更差,这只是总体上很糟糕(quel surprise)。
其他回答
let filled=[];填充长度=10;fill.fill(0);console.log(已填充);
我通常使用Uint8Array(速度惊人)。例如,创建1M个元素的零填充矢量:
var zeroFilled = [].slice.apply(new Uint8Array(1000000))
我是一个Linux用户,一直为我工作,但有一次,一个朋友使用Mac时有一些非零元素。我以为他的机器出故障了,但我们找到了最安全的方法来修复它:
var zeroFilled = [].slice.apply(new Uint8Array(new Array(1000000))
已编辑
铬25.0.1364.160
弗雷德里克·戈特利布-6.43萨姆·巴纳姆-4.83伊莱-3.68约书亚2.91马修·克鲁姆利-2.67硼-2.55艾伦·赖斯-2.11康纳斯-0.68推压器温度-0.67zertosh-错误
Firefox 20.0
艾伦·赖斯-1.85约书亚-1.82马修·克鲁姆利-1.79硼-137弗雷德里克·戈特利布-0.67萨姆·巴纳姆-0.63伊莱-0.59kagax-0.13推压器温度-0.13zertosh-错误
错过了最重要的测试(至少对我来说):Node.js测试。我怀疑它接近Chrome基准。
使用lodash或下划线
_.range(0, length - 1, 0);
或者,如果您已有一个数组,并且需要一个相同长度的数组
array.map(_.constant(0));
递归解决方案
正如其他人所指出的,利用.contat()通常提供快速解决方案。下面是一个简单的递归解决方案:
函数zeroFill(len,a){return len<=(a||(a=[0])).长度?a.切片(0,len):zeroFill(长度,a.concat(a))}console.log(zeroFill(5));
以及一个通用递归数组填充函数:
函数填充(len,v){返回长度<=(v=[].contat(v,v)).length?v.slice(0,len):填充(len,v)}console.log(fill(5,'abc'));
使用对象表示法
var x = [];
零填充?喜欢
var x = [0,0,0,0,0,0];
填充了“undefined”。。。
var x = new Array(7);
带零的obj符号
var x = [];
for (var i = 0; i < 10; i++) x[i] = 0;
作为补充说明,如果修改Array的原型
var x = new Array();
and
var y = [];
将对原型进行修改
无论如何,我不会过分关注这个操作的效率或速度,你可能会做很多其他事情,这些事情比安装一个包含零的任意长度数组更浪费和昂贵。