JS string comparison

Benchmark created on


Preparation HTML

<script>
const actual = ["a", "b", "c", "d", "e"][Math.floor(Math.random() * 6)]

const A = "a";
const B = "b";
const C = "c";
const D = "d";
const E = "e";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Inline comparison
const isActual = (
	actual === "a" ||
	actual === "b" ||
	actual === "c" ||
	actual === "d" ||
	actual === "e"
)
ready
Pointer comparisons
const isActual = (
	actual === A ||
	actual === B ||
	actual === C ||
	actual === D ||
	actual === E
)
ready
Array comparison
const isActual = [A, B, C, D, E].includes(actual)
ready
Regex comparison
const isActual = /^a|b|c|d|e$/.test(actual)
ready

Revisions

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