Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Intl.Collator.compare() vs localeCompare() no options sort
(version: 1)
Comparing performance of:
check localeCompare vs check collator vs sort localeCompare vs sort collator
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = "FOO (BAR)"; var b = "foo bar 2"; var locale = "en"; var randomArray = Array(200).fill().map( () => Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 30) ) var collator = new Intl.Collator(locale);
Tests:
check localeCompare
a.localeCompare(b, locale) === 0
check collator
collator.compare(a, b) === 0
sort localeCompare
[...randomArray].sort((a,b) => a.localeCompare(b, locale))
sort collator
[...randomArray].sort(collator.compare)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
check localeCompare
check collator
sort localeCompare
sort collator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Safari/537.36 OPR/60.3.3004.55692
Browser/OS:
Opera 60 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
check localeCompare
12132.2 Ops/sec
check collator
751183.1 Ops/sec
sort localeCompare
10.4 Ops/sec
sort collator
859.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark, specifically comparing the performance of `Intl.Collator.compare()` and `localeCompare()` without any options or sorting. **What is tested?** Two main functions are being compared: 1. `intl collator.compare(a, b) === 0`: * This function uses the `Intl.Collator` API to compare two strings. * The `compare()` method returns a negative value if string `a` comes before string `b`, zero if they are equal, and a positive value if `a` comes after `b`. 2. `localeCompare(b, locale) === 0`: * This function uses the `localeCompare()` method to compare two strings based on the current locale. * The `localeCompare()` method returns a negative value if string `b` comes before string `a`, zero if they are equal, and a positive value if `b` comes after `a`. **Options compared** In this benchmark, there are no options being compared. Both functions have default settings, which means the results may vary depending on the JavaScript engine, browser, or platform. **Pros and Cons of different approaches** 1. **Intl.Collator.compare()** * Pros: + Provides a more standardized way to compare strings based on locale. + Can be used with multiple collation keys (e.g., sorting). * Cons: + May not work in older browsers or JavaScript engines that don't support Intl. 2. **localeCompare()** * Pros: + Works in most modern browsers and JavaScript engines. + Does not require additional libraries or configuration. * Cons: + May not provide the same level of control as Intl.Collator.compare(). + Can be slower for very large datasets due to locale-specific calculations. **Library used:** The `Intl` library is a part of the ECMAScript standard and provides internationalization (i18n) functions. In this benchmark, `Intl.Collator` is used to access the `compare()` method. **Special JS features or syntax** There are no special JavaScript features or syntax being tested in this benchmark. It's purely focused on comparing the performance of two string comparison methods. **Alternatives:** Other alternatives for string comparison in JavaScript include: 1. **String.prototype.localeCompare()**: Similar to `localeCompare()`, but specific to the current locale. 2. **Array.prototype.sort() with a compare function**: Can be used to sort arrays of strings, but may not provide the same level of control as Intl.Collator.compare(). 3. **Other i18n libraries or polyfills**: Depending on the specific use case, other libraries like Unicode Normalization or ICU might be more suitable. It's worth noting that this benchmark is specifically designed for Chrome Mobile 129 and Android 10 devices. If you're interested in running similar benchmarks on different browsers or platforms, you may need to adjust the script preparation code accordingly.
Related benchmarks:
Intl.Collator.compare() vs localeCompare() - natural sort order
Intl.Collator.compare vs localeCompare with numbers
localCompare vs Intl.Collator
Intl.Collator.compare() lowercase vs sensitivity vs localeCompare
Comments
Confirm delete:
Do you really want to delete benchmark?