0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScript: Arrayの罠

Posted at

Array配下の組み込み関数における注意点

  • 添字が0~4294967294
    mapforEachkeysvalues等で全て読み取れる
  • 添字が負数、小数、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様より高速に動作したものである。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?