regexp vs charCode

Benchmark created on


Setup

let reg = /^[\dA-Z]+$/;

function isDigitOrUppercase(string) {
	for (let j = 0; j < string.length; j++) {
		let isDigit = string.charCodeAt(j) >= 48 && string.charCodeAt(j) <= 57;
		let isUpperCase =
			string.charCodeAt(j) >= 65 && string.charCodeAt(j) <= 90;
		if (!isDigit && !isUpperCase) return false;
	}

	return true;
}

function testWithReg(string) {
    string.match(reg);
}

function testWithCharCode(string) {
    isDigitOrUppercase(string);
}

Test runner

Ready to run.

Testing in
TestOps/sec
With RegExp (worst case)
testWithReg("FOO")
ready
With CharCode (worst case)
testWithCharCode("FOO")
ready
With RegExp (best case)
testWithReg("foo")
ready
With CharCode (best case)
testWithCharCode("foo")
ready

Revisions

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