Case insensitive equals (v3)

Revision 3 of this benchmark created on


Setup

const baseWord = "aBcDeFgHiJkLmNo";

const createMatchingWord = () => Array.from({length: 15}, (_, index) => {
	const letter = String.fromCharCode('a'.charCodeAt(0) + index);
	
	if (Math.random() > 0.5) {
		return letter.toUpperCase();
	}
    return letter;
}).join('');

const matchingWords = Array.from({length: 50000}, createMatchingWord);
 
 
const minCodePoint = 'b'.charCodeAt(0);
const maxCodePoint = 'z'.charCodeAt(0);
const randomLetter = () => {
	const letter = String.fromCharCode(Math.floor(Math.random() * (maxCodePoint - minCodePoint) + minCodePoint));
	
	if (Math.random() > 0.5) {
		return letter.toUpperCase();
	}
    return letter;
};
const createFailingWord = () => Array.from({length: 15}, randomLetter).join('');

const failingWords = Array.from({length: 50000}, createFailingWord);

const run = (equalsFn) => {
  for (const word of matchingWords) {
	if (!equalsFn(baseWord, word)) {
	  throw new Error("Unexpected result");
	}
  }

  for (const word of failingWords) {
	if (equalsFn(baseWord, word)) {
	  throw new Error("Unexpected result");
	}
  }
};

Test runner

Ready to run.

Testing in
TestOps/sec
toUpper strict equals
run((a, b) => a.toUpperCase() === b.toUpperCase());
ready
localeCompare base (note that 'accent' returns similar results)
const options = { sensitivity: "base" };

run((a, b) => a.localeCompare(b, undefined, options) === 0);


ready
Regex
run((a, b) => new RegExp(`^${a}$`, "i").test(b));
ready
Regex pre-prepared
const baseWordRegex = new RegExp(`^${baseWord}$`, "i");
run((_ /* same as baseWord */, b) => baseWordRegex.test(b));
ready
Intl.Collator
const options = { sensitivity: "base" };
const collatorCompare = new Intl.Collator("en", options).compare;
run((a, b) => collatorCompare(a, b) === 0);
ready

Revisions

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