Array
配下の組み込み関数における注意点
- 添字が0~4294967294
map
、forEach
、keys
、values
等で全て読み取れる - 添字が負数、小数、4294967295以上の整数、その他の文字列
Array
配下の組み込み関数では読み取れない。Object.keys
等では読み取れる
let A=[];
A[4294967295]=A[-1]=A[.1]=A.a=0;
console.log(A.length);//0
A.forEach(a=>console.log(a))//nothing
添字が負数の場合は更に深刻な問題がある。Browser次第だが、要素の読み書きが劇的に低速化するという点だ。
for(let a=3,s=String.fromCodePoint;a--;){
console.time("+");
for(let a=[],b=0;b<1e6;)a[b]=b++;
console.timeEnd("+");
console.time("-");
for(let a=[],b=0;b<1e6;)a[~b]=b++;
console.timeEnd("-");
console.time("s");
for(let a=[],b=0;b<1e6;)a[s(b)]=b++;
console.timeEnd("s");
}
添字に負数なんか指定していると、かつてはInternet Explorer 6ごときがGoogle Chrome様より高速に動作したものである。