string case insensitive localeCompare

Benchmark created on


Description

Comparation between string.localeCompare(string, undefined, {sensitivity: 'base'}) and string.toLowerCase().localeCompare(string.toLowerCase())

Setup

function generateRandomString(length) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    let result = '';
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return result;
}

const arrayData = Array.from({ length: 120 }, () => ({
    text: generateRandomString(Math.floor(Math.random() * (30 - 15 + 1)) + 15)
}));

Test runner

Ready to run.

Testing in
TestOps/sec
array sort with localeCompare / sensivity:base
arrayData.sort(function (a, b) {
		return a.text.localeCompare(
			b.text,
			undefined,
			{ sensitivity: 'base' }
		);
	}
);
ready
array sort with localeCompare / toLowerCase
arrayData.sort(function (a, b) {
		return a.text.toLowerCase().localeCompare(
			b.text.toLowerCase()
		);
	}
);
ready

Revisions

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