1
1

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: 比較速度検証

Posted at

!=====XOROR等で配列を長距離比較してみます

TypedArray
{let n=1<<20,A=new Uint8Array(n*2),B=A.subarray(n);
A[n-1]=1;
for(let a=10,b;a--;){
console.time("!==")
	for(b=0;b<n;b+=4)if(A[b]!==B[b]||A[b+1]!==B[b+1]||A[b+2]!==B[b+2]||A[b+3]!==B[b+3])break;
console.timeEnd("!==")
console.time("xor")
	for(b=0;b<n;b+=4)if(A[b]^B[b]||A[b+1]^B[b+1]||A[b+2]^B[b+2]||A[b+3]^B[b+3])break;
console.timeEnd("xor")
console.time("xor2")
	for(b=0;b<n;b+=4)if(A[b]^A[b+1]<<8^A[b+2]<<16^A[b+3]<<24^B[b]^B[b+1]<<8^B[b+2]<<16^B[b+3]<<24)break;
console.timeEnd("xor2")
console.time("===")
	for(b=0;b<n&&A[b]===B[b]&&A[b+1]===B[b+1]&&A[b+2]===B[b+2]&&A[b+3]===B[b+3];b+=4);
console.timeEnd("===")
console.time("or")
	for(b=0;b<n&&(A[b]|A[b+1]<<8|A[b+2]<<16|A[b+3]<<24)===(B[b]|B[b+1]<<8|B[b+2]<<16|B[b+3]<<24);b+=4);
console.timeEnd("or")
}}

まあ御覧のように有意義な差が出ているとは言い難い…

!==: 6.18798828125 ms
xor: 6.696044921875 ms
xor2: 7.81494140625 ms
===: 6.178955078125 ms
or : 8.650146484375 ms

!==: 0.56884765625 ms
xor: 0.68212890625 ms
xor2: 0.38916015625 ms
===: 1.07080078125 ms
or : 0.77001953125 ms

!==: 3.239990234375 ms
xor: 3.69677734375 ms
xor2: 4.4208984375 ms
===: 3.755859375 ms
or : 1.778076171875 ms

!==: 0.172119140625 ms
xor: 0.171142578125 ms
xor2: 0.18603515625 ms
===: 0.177978515625 ms
or : 0.18505859375 ms

!==: 0.1669921875 ms
xor: 0.175048828125 ms
xor2: 0.189208984375 ms
===: 0.173828125 ms
or : 0.18701171875 ms

!==: 0.1689453125 ms
xor: 0.1708984375 ms
xor2: 0.2548828125 ms
===: 0.170166015625 ms
or : 0.18701171875 ms

!==: 0.174072265625 ms
xor: 0.1708984375 ms
xor2: 0.190185546875 ms
===: 0.1689453125 ms
or : 0.18408203125 ms

!==: 0.171142578125 ms
xor: 0.173095703125 ms
xor2: 0.18798828125 ms
===: 0.171142578125 ms
or : 0.18603515625 ms

!==: 0.16796875 ms
xor: 0.172119140625 ms
xor2: 0.191162109375 ms
===: 0.16796875 ms
or : 0.18408203125 ms

!==: 0.177001953125 ms
xor: 0.172119140625 ms
xor2: 0.18701171875 ms
===: 0.1708984375 ms
or : 0.184814453125 ms

続いてDataViewで複数byteまとめて比較

DataView
{let n=1<<20,A=new DataView(new Uint8Array(n*2).buffer);
A.setUint8(n-1,1);
for(let a=10,b;a--;){
console.time("u32!==u32")
	for(b=0;b<n;b+=4)if(A.getUint32(b)!==A.getUint32(b+n))break;
console.timeEnd("u32!==u32")
console.time("u32^u32")
	for(b=0;b<n;b+=4)if(A.getUint32(b)^A.getUint32(b+n))break;
console.timeEnd("u32^u32")
console.time("u32===u32")
	for(b=0;b<n&&A.getUint32(b)===A.getUint32(b+n);b+=4);
console.timeEnd("u32===u32")
console.time("i64!==i64")
	for(b=0;b<n;b+=8)if(A.getBigInt64(b)!==A.getBigInt64(b+n))break;
console.timeEnd("i64!==i64")
}}

初回は明らかにTypedArrayより高速。しかし複数繰り返すうちに差がほとんど無くなる。それでもgetBigInt64による8byte単位の比較は他を圧倒する。loop回数が他の半分になせいだろう

u32!==u32: 4.508056640625 ms
u32^u32: 3.10400390625 ms
u32===u32: 2.045166015625 ms
i64!==i64: 1.177001953125 ms

u32!==u32: 0.93115234375 ms
u32^u32: 1.210205078125 ms
u32===u32: 2.372802734375 ms
i64!==i64: 1.27099609375 ms

u32!==u32: 2.553955078125 ms
u32^u32: 0.990966796875 ms
u32===u32: 1.004150390625 ms
i64!==i64: 0.68115234375 ms

u32!==u32: 2.1318359375 ms
u32^u32: 1.011962890625 ms
u32===u32: 0.89599609375 ms
i64!==i64: 0.68408203125 ms

u32!==u32: 4.91796875 ms
u32^u32: 1.255859375 ms
u32===u32: 0.987060546875 ms
i64!==i64: 0.593017578125 ms

u32!==u32: 2.01904296875 ms
u32^u32: 0.218994140625 ms
u32===u32: 0.095947265625 ms
i64!==i64: 0.052001953125 ms

u32!==u32: 0.069091796875 ms
u32^u32: 0.1298828125 ms
u32===u32: 0.129150390625 ms
i64!==i64: 0.063232421875 ms

u32!==u32: 0.134033203125 ms
u32^u32: 0.14697265625 ms
u32===u32: 0.14990234375 ms
i64!==i64: 0.06103515625 ms

u32!==u32: 0.138916015625 ms
u32^u32: 0.126953125 ms
u32===u32: 0.127197265625 ms
i64!==i64: 0.06103515625 ms

u32!==u32: 0.134033203125 ms
u32^u32: 0.127197265625 ms
u32===u32: 0.158935546875 ms
i64!==i64: 0.06103515625 ms

Chrome 131.0.6778.205で検証

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?