JavaScript: 疎配列の巡回速度検証

ここで言う疎配列とは要素が飛び飛びに埋め込まれた配列の事。例えば以下のような配列
```js
a=[,1,,22,,,,333,,,,,,,,55555]
```
疎配列の使い所さんと言えばhash表。このような配列の要素をもれなく読みあさる速度を検証するのが今回の企画。`for in`と`forEach`で対決して頂く。
```js
for(let n=9,a=256;n--;a+=a)for(let b=32;b;b>>=1){
	let A=[],t=0,u,c=0;
	for(;t<a;t+=b)A[t]=0,c++;//埋め込み

	t=performance.now();
	for(let c=32,d;c--;)for(d in A);
	t=performance.now()-t;

	u=performance.now();
	for(let c=32,d;c--;)A.forEach((a,b)=>a);
	u=performance.now()-u;
	console.log("size",a,"interval",b-1,"items",c,"time",t,u)
}
```
以下の表に示す**間隔**とは、要素同士の隙間。0というのはもはや疎配列ではない…と突っ込みたくなるが気にしてはいけない。所要時間の単位はms、Browserはchrome。
御覧の通り概ね`forEach`が優勢(Firefoxも同様)。しかしなぜか配列長が32768、65536、間隔が31の時の`forEach`がべらぼうに遅い。ちなみに65536を超えてからも間隔が31以上だと`forEach`は急激に遅くなる
<table>
<tr><th>配列長<th>間隔<th>要素数<th>for in<th>forEach
<tr><td>256<td>31<td>8<td>0.199<td>0.100
<tr><td>256<td>15<td>16<td>0<td>0.200
<tr><td>256<td>7<td>32<td>0.200<td>0
<tr><td>256<td>3<td>64<td>0.100<td>0.099
<tr><td>256<td>1<td>128<td>0.299<td>0.100
<tr><td>256<td>0<td>256<td>0.5<td>0.299
<tr><td>512<td>31<td>16<td>0.200<td>0
<tr><td>512<td>15<td>32<td>0.200<td>0.099
<tr><td>512<td>7<td>64<td>0.200<td>0.099
<tr><td>512<td>3<td>128<td>0.900<td>0.299
<tr><td>512<td>1<td>256<td>0.599<td>0.299
<tr><td>512<td>0<td>512<td>1.100<td>0.099
<tr><td>1024<td>31<td>32<td>0.299<td>0
<tr><td>1024<td>15<td>64<td>1.200<td>0.099
<tr><td>1024<td>7<td>128<td>0.399<td>0
<tr><td>1024<td>3<td>256<td>0.600<td>0.099
<tr><td>1024<td>1<td>512<td>1<td>0.300
<tr><td>1024<td>0<td>1024<td>1.899<td>0.100
<tr><td>2048<td>31<td>64<td>0.599<td>0.200
<tr><td>2048<td>15<td>128<td>1.100<td>0.199
<tr><td>2048<td>7<td>256<td>0.700<td>0.299
<tr><td>2048<td>3<td>512<td>1.100<td>0.299
<tr><td>2048<td>1<td>1024<td>2.200<td>0.200
<tr><td>2048<td>0<td>2048<td>3.900<td>0.399
<tr><td>4096<td>31<td>128<td>0.700<td>0.400
<tr><td>4096<td>15<td>256<td>1.299<td>0.400
<tr><td>4096<td>7<td>512<td>1.299<td>0.5
<tr><td>4096<td>3<td>1024<td>2.600<td>0.399
<tr><td>4096<td>1<td>2048<td>3.900<td>0.400
<tr><td>4096<td>0<td>4096<td>7.5<td>0.700
<tr><td>8192<td>31<td>256<td>1.699<td>0.200
<tr><td>8192<td>15<td>512<td>2.200<td>0.099
<tr><td>8192<td>7<td>1024<td>2.900<td>0.099
<tr><td>8192<td>3<td>2048<td>4.600<td>0.200
<tr><td>8192<td>1<td>4096<td>7.299<td>0.200
<tr><td>8192<td>0<td>8192<td>14<td>0.200
<tr><td>16384<td>31<td>512<td>3.700<td>0.200
<tr><td>16384<td>15<td>1024<td>4.799<td>0.299
<tr><td>16384<td>7<td>2048<td>6.099<td>0.299
<tr><td>16384<td>3<td>4096<td>9.100<td>0.299
<tr><td>16384<td>1<td>8192<td>14.899<td>0.5
<tr><td>16384<td>0<td>16384<td>28.200<td>0.299
<tr><td>32768<td>31<td>1024<td>10.100<td>68.599
<tr><td>32768<td>15<td>2048<td>17.400<td>9.799
<tr><td>32768<td>7<td>4096<td>25<td>10.199
<tr><td>32768<td>3<td>8192<td>52.700<td>13
<tr><td>32768<td>1<td>16384<td>83.100<td>20.5
<tr><td>32768<td>0<td>32768<td>160.799<td>30.5
<tr><td>65536<td>31<td>2048<td>20.799<td>142.399
<tr><td>65536<td>15<td>4096<td>29.299<td>17.100
<tr><td>65536<td>7<td>8192<td>65.900<td>19.099
<tr><td>65536<td>3<td>16384<td>77.099<td>23.799
<tr><td>65536<td>1<td>32768<td>156.5<td>35.5
<tr><td>65536<td>0<td>65536<td>296.400<td>49.200</table>

`for of`は疎部まで読みあさり、`map`は無駄な複製処理を行いますが、ついでなので検証
```js
for(let n=9,a=256;n--;a+=a)for(let b=32;b;b>>=1){
	let A=[],t=0,u,v,w,c=0;
	for(;t<a;t+=b)A[t]=0,c++;//埋め込み

	t=performance.now();
	for(let c=32,d;c--;)for(d in A);
	t=performance.now()-t;

	u=performance.now();
	for(let c=32,d;c--;)A.forEach((a,b)=>a);
	u=performance.now()-u;

	v=performance.now();
	for(let c=32,d;c--;)A.map((a,b)=>a);
	v=performance.now()-v;

	w=performance.now();
	for(let c=32,d;c--;)for(d of A);
	w=performance.now()-w;

	console.log("size",a,"interval",b-1,"items",c,"time",t,u,v,w)
}
```

<table>
<tr><th>配列長<th>間隔<th>要素数<th>for in<th>forEach<th>map<th>for of
<tr><td>256<td>31<td>8<td>0<td>0.200<td>0.099<td>0.800
<tr><td>256<td>15<td>16<td>0.099<td>0.200<td>0.099<td>0.900
<tr><td>256<td>7<td>32<td>0.099<td>0.100<td>0.099<td>0.799
<tr><td>256<td>3<td>64<td>0.199<td>0.200<td>0.100<td>1.799
<tr><td>256<td>1<td>128<td>1.5<td>0.200<td>0.199<td>1
<tr><td>256<td>0<td>256<td>0.600<td>0.399<td>0.300<td>0.899
<tr><td>512<td>31<td>16<td>0.200<td>0.200<td>0.299<td>1.799
<tr><td>512<td>15<td>32<td>0.700<td>0.099<td>0.300<td>1.399
<tr><td>512<td>7<td>64<td>0.299<td>0.200<td>0.200<td>1.700
<tr><td>512<td>3<td>128<td>0.299<td>0.300<td>0.299<td>1.799
<tr><td>512<td>1<td>256<td>0.599<td>0.400<td>0.400<td>1.799
<tr><td>512<td>0<td>512<td>1.099<td>0.799<td>0.700<td>1.700
<tr><td>1024<td>31<td>32<td>0.200<td>0.299<td>0.400<td>1.799
<tr><td>1024<td>15<td>64<td>0.199<td>0.400<td>1.799<td>1.299
<tr><td>1024<td>7<td>128<td>0.800<td>0.399<td>0.400<td>4.200
<tr><td>1024<td>3<td>256<td>0.699<td>0.5<td>1<td>1.300
<tr><td>1024<td>1<td>512<td>1<td>0.599<td>0.800<td>1.299
<tr><td>1024<td>0<td>1024<td>2<td>0.899<td>0.900<td>1.400
<tr><td>2048<td>31<td>64<td>0.5<td>0.599<td>0.700<td>3.899
<tr><td>2048<td>15<td>128<td>0.5<td>0.599<td>0.700<td>3
<tr><td>2048<td>7<td>256<td>0.799<td>0.5<td>0.799<td>3
<tr><td>2048<td>3<td>512<td>1.399<td>0.700<td>0.900<td>4.700
<tr><td>2048<td>1<td>1024<td>2.400<td>0.900<td>1.199<td>2.700
<tr><td>2048<td>0<td>2048<td>4.299<td>1.900<td>2<td>2.5
<tr><td>4096<td>31<td>128<td>0.799<td>1<td>1.399<td>4.300
<tr><td>4096<td>15<td>256<td>1<td>0<td>0.799<td>0.100
<tr><td>4096<td>7<td>512<td>1.399<td>0.100<td>0.899<td>0.200
<tr><td>4096<td>3<td>1024<td>2.299<td>0<td>0.799<td>0
<tr><td>4096<td>1<td>2048<td>3.700<td>0.099<td>0.800<td>0.099
<tr><td>4096<td>0<td>4096<td>7.099<td>0.100<td>0.599<td>0.200
<tr><td>8192<td>31<td>256<td>1.899<td>0.200<td>1.400<td>0.200
<tr><td>8192<td>15<td>512<td>2.200<td>0.099<td>1.400<td>0.299
<tr><td>8192<td>7<td>1024<td>2.700<td>0.200<td>1.399<td>0.200
<tr><td>8192<td>3<td>2048<td>4.700<td>0.200<td>1.299<td>0.299
<tr><td>8192<td>1<td>4096<td>7.5<td>0.200<td>1.5<td>0.199
<tr><td>8192<td>0<td>8192<td>14.5<td>0.299<td>1.5<td>0.299
<tr><td>16384<td>31<td>512<td>3.399<td>0.400<td>2.799<td>0.400
<tr><td>16384<td>15<td>1024<td>5<td>0.199<td>2.900<td>0.5
<tr><td>16384<td>7<td>2048<td>6.5<td>0.300<td>2.599<td>0.400
<tr><td>16384<td>3<td>4096<td>8.700<td>0.299<td>2.5<td>0.5
<tr><td>16384<td>1<td>8192<td>15.900<td>0.299<td>3<td>0.399
<tr><td>16384<td>0<td>16384<td>30<td>0.200<td>3.599<td>0.799
<tr><td>32768<td>31<td>1024<td>10.199<td>76.5<td>72.800<td>82.899
<tr><td>32768<td>15<td>2048<td>14.5<td>8.600<td>11.5<td>50.200
<tr><td>32768<td>7<td>4096<td>25.200<td>9.700<td>12.299<td>40.900
<tr><td>32768<td>3<td>8192<td>46.700<td>11.699<td>15.400<td>36.599
<tr><td>32768<td>1<td>16384<td>88.5<td>17.5<td>25.400<td>37.299
<tr><td>32768<td>0<td>32768<td>157.799<td>28.700<td>40.5<td>35.899
<tr><td>65536<td>31<td>2048<td>21.799<td>142.200<td>151.299<td>151.200
<tr><td>65536<td>15<td>4096<td>28.299<td>16.5<td>29.399<td>92.900
<tr><td>65536<td>7<td>8192<td>59.200<td>19.399<td>31.299<td>76.300
<tr><td>65536<td>3<td>16384<td>79.299<td>25<td>39<td>75.200
<tr><td>65536<td>1<td>32768<td>156.599<td>34.200<td>50.200<td>70.700
<tr><td>65536<td>0<td>65536<td>292.700<td>51.799<td>74.400<td>66.099</table>