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 methodArray.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 assignmentArray.prototype.init2 = function(x,n){if(typeof(n)=='undefined') { n = this.length; }while (n--) { this.push(x); }return this;}
//joshua's methodfunction 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();
function newFilledArray(len, val) {var a = [];while(len--){a.push(val);}return a;}
var st = (new Date()).getTime();newFilledArray(1000000, 0)console.log((new Date()).getTime() - st); // returned 63, 65, 62 milliseconds
var a = [];var len = 1000000;var st = (new Date()).getTime();while(len){a.push(0);len -= 1;}console.log((new Date()).getTime() - st); // returned 863, 894, 875 millisecondsst = (new Date()).getTime();len = 1000000;a = [];for(var i = 0; i < len; i++){a.push(0);}console.log((new Date()).getTime() - st); // returned 1155, 1179, 1163 milliseconds
var current_year = new Date().getFullYear();var ages_array = new Array();
for (var i in data) {if(data[i]['BirthDate'] != null && data[i]['BirthDate'] != '0000-00-00'){
var birth = new Date(data[i]['BirthDate']);var birth_year = birth.getFullYear();var age = current_year - birth_year;
if(ages_array[age] == null){ages_array[age] = 1;}else{ages_array[age] += 1;}
}}
console.log(ages_array);
function P(n) {return Array.apply(null, Array(n)).map(Number.prototype.valueOf,0);}
try {P(1000000);} catch(e) {console.error(e.message);}
代码和示例
下面的代码介绍了测量中使用的解决方案
function A(n) {return [...new Array(n)].fill(0);}
function B(n) {return new Array(n).fill(0);}
function C(n) {return Array(n).fill(0);}
function D(n) {return Array.from({length: n}, () => 0);}
function E(n) {return [...new Array(n)].map(x => 0);}
// arrays with type
function F(n) {return Array.from(new Int32Array(n));}
function G(n) {return Array.from(new Float32Array(n));}
function H(n) {return Array.from(new Float64Array(n)); // needs 2x more memory than float32}
function I(n) {return new Float32Array(n); // this is not typical array}
function J(n) {return [].slice.apply(new Float32Array(n));}
// Based on for
function K(n) {let a = [];a.length = n;let i = 0;while (i < n) {a[i] = 0;i++;}return a;}
function L(n) {let a=[]; for(let i=0; i<n; i++) a[i]=0;return a;}
function M(n) {let a=[]; for(let i=0; i<n; i++) a.push(0);return a;}
function N(n) {let a = new Array(n); for (let i=0; i<n; ++i) a[i] = 0;return a;}
function O(n) {let a = new Array(n); for (let i=n; i--;) a[i] = 0;return a;}
// other
function P(n) {return Array.apply(null, Array(n)).map(Number.prototype.valueOf,0);}
function Q(n) {return "0".repeat( n ).split("").map( parseFloat );}
function R(n) {return new Array(n+1).join('0').split('').map(parseFloat)}
// ---------// TEST// ---------[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R].forEach(f => {let a = f(10);console.log(`${f.name} length=${a.length}, arr[0]=${a[0]}, arr[9]=${a[9]}`)});
//**To create an all new Array**
console.log(new Array(5).fill(0));
//**To add some values at the end of an existing Array**
let existingArray = [1,2,3]
console.log([...existingArray, ...new Array(5).fill(0)]);