Find if contains char (v2)

Revision 2 of this benchmark created on


Setup

const s = "abcdefghij.pippo#pluto";
let ris;

Test runner

Ready to run.

Testing in
TestOps/sec
Regex test
for (let i = 0; i < 1000; i++)
	ris = /[.#]/.test(s);
ready
includes
for (let i = 0; i < 1000; i++)
{
	ris = false;
	for (let j = 0; j < s.length; j++)
		if ("#.".includes(s[j]))
		{
			ris = true;
			break;
		}
}
	
ready
Includes fixed string
const c = "#.";
for (let i = 0; i < 1000; i++)
{
	ris = false;
	for (let j = 0; j < s.length; j++)
		if (c.includes(s[j]))
		{
			ris = true;
			break;
		}
}
	
ready
Manual if
for (let i = 0; i < 1000; i++)
{
	ris = false;
	for (let j = 0; j < s.length; j++)
		if (s[j] === "#" || s[j] === ".")
		{
			ris = true;
			break;
		}
}
	
ready
Inverse includes
for (let i = 0; i < 1000; i++)
	ris = s.includes("#") || s.includes(".");
ready
Inverse includes bigger
for (let i = 0; i < 1000; i++)
	ris = s.includes(" ") || s.includes(">") || s.includes("+") || s.includes("~");
ready
Regex test bigger
for (let i = 0; i < 1000; i++)
	ris = /[ >+~]/.test(s);
ready
Indexof from 1
for (let i = 0; i < 1000; i++)
	ris = s.indexOf("#", 1) !== -1 || s.indexOf(".", 1) !== -1;
ready
Includes from 1
for (let i = 0; i < 1000; i++)
{
	const sub = s.substring(1);
	ris = sub.includes("#") || sub.includes(".");
}
ready
Test from 1
for (let i = 0; i < 1000; i++)
{
	const reg = /[.#]/g;
	reg.lastIndex = 1;
	ris = reg.test(s);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.