Exec vs manual finding (v4)

Revision 4 of this benchmark created on


Setup

const s = "tagName.className.class2#id";

Test runner

Ready to run.

Testing in
TestOps/sec
Manual finding
let x = 0;
for (let i = 0; i < s.length; i++)
	if ("#.".includes(s[i]))
		x++;
ready
Exec
const regex = /[.#]/g;
let x = 0;
let match = regex.exec(s);
while (match !== null)
{
	x++;
	match = regex.exec(s);
}

ready
indexOf
let x = 0;

let indexOfId = s.indexOf("#");
let indexOfClass = s.indexOf(".");
let startIndex = indexOfClass === -1 || indexOfId !== -1 && indexOfId < indexOfClass ? indexOfId : indexOfClass;
while (startIndex !== -1)
{
	x++;
	indexOfId = s.indexOf("#", startIndex + 1);
	indexOfClass = s.indexOf(".", startIndex + 1);
	startIndex = indexOfClass === -1 || indexOfId !== -1 && indexOfId < indexOfClass ? indexOfId : indexOfClass;
}
ready
Search
let x = 0;
let index = s.search(/[.#]/);
let tmp = s;
while (index !== -1)
{
	x++;
	tmp = tmp.substring(index + 1);
	index = tmp.search(/[.#]/);
}
ready

Revisions

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