Check for empty string

Benchmark created on


Setup

const strings = [
	"",
	"abc",
	"0",
	"x".repeat(1000000)
];

let empty = 0;

Teardown

console.assert(empty == 1);

Test runner

Ready to run.

Testing in
TestOps/sec
string.length == 0
for (const s of strings) {
	if (s.length == 0) empty++;
}
ready
string.length === 0
for (const s of strings) {
	if (s.length === 0) empty++;
}
ready
string == ""
for (const s of strings) {
	if (s == "") empty++;
}
ready
string === ""
for (const s of strings) {
	if (s === "") empty++;
}
ready
implicit string to boolean conversion
for (const s of strings) {
	if (!s) empty++;
}
ready
accessing the first char via array index
for (const s of strings) {
	if (s[0] === undefined) empty++;
}
ready
accessing the first char via string.at(0)
for (const s of strings) {
	if (s.at(0) === undefined) empty++;
}
ready

Revisions

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