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?

正規表現とか気になる検証

Posted at

正規表現をliteralではなく変数で表現する場合、幾つか方法がありますが、果たして速度にどれほどの差があるのでしょうか。RegExpを筆頭にevalFunctionで検証してみます。

for(let a=4,n=1e4,v0="(?:)",v1="/(?:)/g",v2="return/(?:)/g";a--;){
	console.time`Reg`;
	for(let a=n;a--;)RegExp(v0,"g");
	console.timeEnd`Reg`
	console.time`eval`;
	for(let a=n;a--;)eval(v1);
	console.timeEnd`eval`;
	console.time`fn`;
	for(let a=n;a--;)Function(v2)();
	console.timeEnd`fn`;
}

Functionは全く使い物にならない程度に圧倒的な低速っぷり。これは想定内です。evalFunctionを遥かに凌駕しない程度の速度を誇ります。RegExpはそんなevalより数十倍高速です。evalにはもっと期待していたので残念と言う他ありません。なぜならcode golfにevalは欠かせないのだ!

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?