Fastest way to check first character of a string (v3)

Revision 3 of this benchmark created on


Description

regex vs bracket notation vs charAt

Preparation HTML

<script>
window.testStrings = [
	"/absolute/path",
	"relative/path",
	"/another/absolute/path",
	"https",
	"http"
];
</script>

Setup


Test runner

Ready to run.

Testing in
TestOps/sec
regex
const hasSlashAsCharacterZero = /^\//;

window.testStrings.forEach(path => {
	path.match(hasSlashAsCharacterZero);
});
ready
bracket notation
window.testStrings.forEach(path => {
	path[0] === '/';
});
ready
charAt
window.testStrings.forEach(path => {
	path.charAt(0) === '/';
});
ready

Revisions

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