Regex vs charCode vs Set

Benchmark created on


Setup

const test = '1234567890'
const set = new Set(['1','2','3','4','5','6','7','8','9','0'])
const regex = /^\d{10}$/

Test runner

Ready to run.

Testing in
TestOps/sec
Set
let result = false;
for (let i of test) {
	result = set.has(i);
	if (!result) break;
}
ready
Regex
let result = regex.test(test)
ready
Char code
let result = false;
for (let i = 0; i < test.length; i++) {
	let code = test.charCodeAt(i)
	result = code > 47 && code < 58;
	if (!result) break;
}
ready

Revisions

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