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: 文字、連想配列、Mapにまつわる速度検証

Posted at

文字読み取り法比較

文字列から1文字読み取る速度を比較。text.charAt(index)text[index]textToArray[index](文字列を配列化)の3つで比較。

const cs=65536, run=1024, S=Array(cs+1).join(0), T=S.split``;
for(let a=4;a--;){
	console.time`Array`
	for(let a=run;a--;)for(let b=cs,c;b;)c=T[--b];
	console.timeEnd`Array`
	console.time`[]`
	for(let a=run;a--;)for(let b=cs,c;b;)c=S[--b];
	console.timeEnd`[]`
	console.time`charAt`
	for(let a=run;a--;)for(let b=cs,c;b;)c=S.charAt(--b);
	console.timeEnd`charAt`
}

配列化してから読み取るほうが断然高速っぽい。昔ながらのcharAtは最低速。

数値→文字

数値を差し上げるから文字をよこせ的な処理の速度比較検証をしてみます。String.fromCharCodeで文字召喚、Arrayから読み取り、Mapから読み取りの3つを比較。

const cs=65536, run=256, S=String.fromCharCode, A=Array(cs), M=new Map;
for(let a=cs;a;)M.set(--a,A[a]=S(a));//まず格納
for(let a=4;a--;){
	console.time`関数`;
	for(let a=run;a--;)for(let b=cs,c;b;)c=S(--b);
	console.timeEnd`関数`;
	console.time`配列`;
	for(let a=run;a--;)for(let b=cs,c;b;)c=A[--b];
	console.timeEnd`配列`;
	console.time`Map`;
	for(let a=run;a--;)for(let b=cs,c;b;)c=M.get(--b);
	console.timeEnd`Map`
}

Mapが圧倒的に断然ダントツぶっちぎりで抜群に遅いです。なんと他より10倍以上の差をつけてしまいました! ArrayString.fromCharCodeは僅差です。

文字→数値

文字を与えてやるから数値よこせ的な処理速度比較。charCodeAtMap{}の3つで比較。

固定長
const cs=65536, run=256, S=String.fromCharCode, A=Array(cs), H={}, M=new Map;
for(let a=cs,b;a;)M.set(b=A[--a]=S(a),H[b]=a);//格納
for(let a=4, T=A.join``;a--;){
	console.time`charCode`;
	for(let a=run;a--;)for(let b=cs,c;b;)c=T.charCodeAt(--b);
	console.timeEnd`charCode`;
	console.time`hash`;
	for(let a=run;a--;)for(let b=cs,c;b;)c=H[A[--b]];
	console.timeEnd`hash`;
	console.time`map`;
	for(let a=run;a--;)for(let b=cs,c;b;)c=M.get(A[--b]);
	console.timeEnd`map`
}
不定長
for(let cs=256, run=1<<14;cs<65537&&run;cs*=2,run>>=1){
	const S=String.fromCharCode, A=Array(cs), H={}, M=new Map;
	for(let a=cs,b;a;)M.set(b=A[--a]=S(a),H[b]=a);//格納
	for(let a=4, T=A.join``;a--;){
		let t=new Date,u,v;
		for(let a=run;a--;)for(let b=cs,c;b;)c=T.charCodeAt(--b);
		u=new Date;t=u-t;
		for(let a=run;a--;)for(let b=cs,c;b;)c=H[A[--b]];
		v=new Date;u=v-u;
		for(let a=run;a--;)for(let b=cs,c;b;)c=M.get(A[--b]);
		console.log(run,"",cs,"文字, cC",t,"hash",u,"map",new Date-v);
	}
}

Mapが最高速となるっぽい。連想配列の場合、keyが増えるほど低速化が顕著。

文字列→数値

これは格納速度と読み取り速度別々に計測。

const cs=1<<12, ss=256, run=64, S=String.fromCharCode, R=Math.random, A=Array(cs);
//build random text
for(let a=cs,b,c;a;A[--a]=c)
	for(b=R()*ss+1|0,c="";b--;)c+=S(R()*cs);

//格納速度
for(let a=4;a--;){
	console.time`M`;
	for(let b=run;b--;)for(let c=cs,M=new Map;c;)M.set(A[--c],c);
	console.timeEnd`M`;
	console.time`H`;
	for(let b=run;b--;)for(let c=cs,H=new Object;c;)H[A[--c]]=c;
	console.timeEnd`H`;
}
//読み取り速度
const M=new Map, H={};
for(let a=cs,b;a;)M.set(b=A[--a],H[b]=a);//まず格納
for(let a=4;a--;){
	console.time`M`;
	for(let b=run;b--;)for(let c=cs,d;c;)d=M.get(A[--c]);
	console.timeEnd`M`;
	console.time`H`;
	for(let b=run;b--;)for(let c=cs,d;c;)d=H[A[--c]];
	console.timeEnd`H`;
}

Mapはkeyの文字数が増えても速度変化が緩やか。連想配列より遥かに高速。

検証環境

Windows7 Google Chrome 109.0.5414.120

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?