Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
collatorCompare() vs collatorCompare2()
(version: 0)
Comparing performance of:
collatorCompare2() vs collatorCompare()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = "FOO BAR"; var b = "foo bar"; var options = { numeric: true }; var collator = new Intl.Collator('en', options); function collatorCompare(valueA, valueB) { const valueAMissing = valueA == null || valueA == undefined; const valueBMissing = valueB == null || valueB == undefined; if (valueA === valueB) { return 0 } else if (valueAMissing) { return -1 } else if (valueBMissing) { return 1 } return collator.compare(valueA, valueB) } function collatorCompare2(valueA, valueB) { const valueAMissing = valueA == null || valueA == undefined; const valueBMissing = valueB == null || valueB == undefined; if (valueA === valueB) { return 0 } if (valueAMissing) { return -1 } if (valueBMissing) { return 1 } return collator.compare(valueA, valueB) }
Tests:
collatorCompare2()
collatorCompare2(a,b) === 0
collatorCompare()
collatorCompare(a,b) === 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
collatorCompare2()
collatorCompare()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
collatorCompare2()
3135905.0 Ops/sec
collatorCompare()
3113233.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Definition** The benchmark is designed to compare two functions: `collatorCompare()` and `collatorCompare2()`. These functions are responsible for comparing two strings using Unicode collation rules. **What is tested?** In this benchmark, we're interested in measuring which function returns 0 when given two input strings. This effectively tests the behavior of each function when the input strings are equal or when one string is missing (null or undefined) but the other is not. **Options compared** The two functions being compared have some differences: * `collatorCompare()` and `collatorCompare2()` differ in how they handle the case where one input string is missing. + `collatorCompare()`: Returns -1 when the first input string is missing, but only if both inputs are missing (i.e., neither of them exists). Otherwise, it returns 0. + `collatorCompare2()`: Returns -1 as soon as it encounters a missing input string. This means that even if one input string is missing and the other is not, `collatorCompare2()` will return -1 instead of 0. **Pros and cons** The choice between these two approaches depends on your specific use case: * `collatorCompare()`: If you want to allow for missing inputs, but still ensure that equal strings are correctly identified as such. However, this might lead to inconsistencies when one input string is missing while the other is not. * `collatorCompare2()`: This approach returns -1 more consistently and predictably, especially when dealing with missing inputs. **Library** The benchmark uses the `Intl.Collator` API from the ECMAScript standard. This library provides Unicode collation functionality for comparing strings in different languages. **Special JS feature or syntax** There is no special JavaScript feature or syntax mentioned here. However, it's worth noting that the `Intl.Collator` API has some additional features and options not shown in this benchmark. **Other alternatives** If you were to rewrite this benchmark without using `Intl.Collator`, you might consider: * Using a custom implementation of Unicode collation rules * Utilizing a library like `unicode-normalization` or `collator-js` * Implementing your own string comparison function using bitwise operations and character codes
Related benchmarks:
?. operator vs. getProperty
if(!variable) vs if(variable===undefined) performance
void 0 vs undefined vs variable containing undefined
compare boolean values v0.1
ParseInt vs conditional ~~
Comments
Confirm delete:
Do you really want to delete benchmark?