var a = 'FOO BAR';
var b = 'foo bar';
var locale1 = 'en-US'
var locale2 = 'tr'
var collator1 = new Intl.Collator(locale1, { sensitivity: 'accent' });
var collator2 = new Intl.Collator(locale2, { sensitivity: 'accent' });
a.toLocaleLowerCase(locale1) === b.toLocaleLowerCase(locale1)
collator1.compare(a, b) === 0
a.toLocaleLowerCase(locale2) === b.toLocaleLowerCase(locale2)
collator2.compare(a, b) === 0
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
toLocaleLowerCase (en-US) | |
collator (en-US) | |
toLocaleLowerCase (tr) | |
collator (tr) |
Test name | Executions per second |
---|---|
toLocaleLowerCase (en-US) | 6005858.5 Ops/sec |
collator (en-US) | 6010899.5 Ops/sec |
toLocaleLowerCase (tr) | 1388352.1 Ops/sec |
collator (tr) | 4917228.5 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark compares the performance of two approaches: toLocaleLowerCase
and an internationalization library called Intl.Collator
. The test cases measure the execution speed of these approaches for both English (en-US) and Turkish (tr) locales.
Script Preparation Code
The script preparation code sets up the environment for the benchmark. It defines:
a
and b
, containing the same text but with different casing (FOO BAR
and foo bar
).locale1
(en-US) and locale2
(tr).collator1
and collator2
. These objects are created using the Intl.Collator
API, which is a built-in JavaScript library for internationalization.Html Preparation Code
There is no HTML preparation code provided, which means that the benchmark does not involve any HTML-related operations or interactions.
Individual Test Cases
The benchmark consists of four test cases, each measuring the performance of one of the two approaches:
toLocaleLowerCase (en-US)
: Measures the execution speed of the toLocaleLowerCase
method for English.collator (en-US)
: Measures the execution speed of the Intl.Collator
library for English.toLocaleLowerCase (tr)
: Measures the execution speed of the toLocaleLowerCase
method for Turkish.collator (tr)
: Measures the execution speed of the Intl.Collator
library for Turkish.Performance Comparison
The benchmark measures the number of executions per second (ExecutionsPerSecond) for each test case, which indicates the relative performance of the two approaches.
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
toLocaleLowerCase
:Intl.Collator
:Intl.Collator Library
The Intl.Collator
library is a built-in JavaScript API that provides support for internationalization and localization. It allows developers to work with different languages, scripts, and locales in a consistent and reliable way. The library offers various features beyond just casing, such as:
Other Alternatives
If you need more advanced internationalization or localization capabilities, consider the following alternatives:
Keep in mind that these alternatives may have different performance characteristics, feature sets, or learning curves compared to the Intl.Collator
library.