Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Intl.Collator.compare('en', {numeric: true}) vs agGridComparator()
(version: 0)
Comparing performance of:
agGridComparator() vs Intl.Collator.compare()
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 agGridComparator(valueA, valueB, accentedCompare = false) { const valueAMissing = valueA == null; const valueBMissing = valueB == null; // this is for aggregations sum and avg, where the result can be a number that is wrapped. // if we didn't do this, then the toString() value would be used, which would result in // the strings getting used instead of the numbers. if (valueA && valueA.toNumber) { valueA = valueA.toNumber(); } if (valueB && valueB.toNumber) { valueB = valueB.toNumber(); } if (valueAMissing && valueBMissing) { return 0; } if (valueAMissing) { return -1; } if (valueBMissing) { return 1; } function doQuickCompare(a, b) { return (a > b ? 1 : (a < b ? -1 : 0)); } if (typeof valueA !== 'string') { return doQuickCompare(valueA, valueB); } if (!accentedCompare) { return doQuickCompare(valueA, valueB); } try { // using local compare also allows chinese comparisons return valueA.localeCompare(valueB); } catch (e) { // if something wrong with localeCompare, eg not supported // by browser, then just continue with the quick one return doQuickCompare(valueA, valueB); } }
Tests:
agGridComparator()
agGridComparator(a,b) === 0
Intl.Collator.compare()
collator.compare(a, b) === 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
agGridComparator()
Intl.Collator.compare()
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
agGridComparator()
9591932.0 Ops/sec
Intl.Collator.compare()
5819347.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents two individual test cases for measuring the performance of JavaScript microbenchmarks. **First Test Case: agGridComparator()** * **Purpose:** This test case measures the performance of the `agGridComparator()` function, which is a custom comparator function designed for aggregations in ag-Grid, a popular JavaScript grid component. * **Options compared:** The test case compares the execution time of the `agGridComparator()` function with and without the `accentedCompare` parameter set to `true`. + With `accentedCompare` set to `true`, the function uses the `localeCompare()` method from the Intl.Collator API, which takes into account the accentuation of characters. + Without `accentedCompare` set to `true`, the function falls back to a simple string comparison using the `doQuickCompare()` function. * **Pros and Cons:** + With `accentedCompare` enabled: - Pros: More accurate comparisons, especially for non-ASCII strings. - Cons: May be slower due to the additional locale-specific calculations. + Without `accentedCompare` enabled: - Pros: Faster execution time, but may produce incorrect results for non-ASCII strings. - Cons: Less accurate comparisons, which can lead to incorrect results or data corruption. * **Library:** None. The test case uses only built-in JavaScript functionality. **Second Test Case: Intl.Collator.compare()** * **Purpose:** This test case measures the performance of the `Intl.Collator.compare()` function from the Intl API. * **Options compared:** The test case compares the execution time of the `Intl.Collator.compare()` function with and without setting the `numeric` option to `true`. + With `numeric` set to `true`, the function performs numerical comparisons only (e.g., comparing numbers, not strings). + Without `numeric` set to `true`, the function falls back to a default behavior that compares strings using the locale-specific rules. * **Pros and Cons:** + With `numeric` enabled: - Pros: Faster execution time for numerical comparisons. - Cons: May produce incorrect results for non-numerical string comparisons. + Without `numeric` enabled: - Pros: More accurate comparisons, but may be slower due to the additional locale-specific calculations. - Cons: Slower execution time compared to the `numeric` option. * **Library:** The Intl API, which is part of the ECMAScript Standard. **Other Alternatives:** 1. Using a different comparator function or implementation for aggregations in ag-Grid. 2. Enabling or disabling locale-specific calculations in the Intl.Collator API. 3. Implementing custom string comparison logic using regular expressions or other techniques. 4. Using a different JavaScript engine or runtime environment, which may optimize certain functions differently. In summary, these test cases measure the performance of two distinct JavaScript microbenchmarks: one for a custom comparator function and another for an Intl API function. The results provide insight into the trade-offs between accuracy, speed, and optimization options when working with strings in JavaScript.
Related benchmarks:
collatorCompare() vs collatorCompare2()
collatorCompare() vs agGridComparator()
collatorCompare() vs collatorCompare2() with isBlank
agGrid vs Collator
Comments
Confirm delete:
Do you really want to delete benchmark?