Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
collatorCompare() vs agGridComparator()
(version: 0)
Comparing performance of:
agGridComparator() 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 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
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
agGridComparator()
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
agGridComparator()
9569837.0 Ops/sec
collatorCompare()
3064937.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its various aspects. **Benchmark Definition** The benchmark compares two JavaScript functions: `agGridComparator()` and `collatorCompare()`. Both functions are used for comparing strings, but they use different approaches to handle null or missing values. **Options Compared** There are three options compared in this benchmark: 1. **`agGridComparator()`**: This function uses the `Intl.Collator` API to compare strings. It first checks if either of the input values is missing (null or undefined), and if so, returns a specific value (-1 or 1). Then, it calls the `compare()` method on the `Collator` object to perform the actual comparison. 2. **`collatorCompare()`**: This function also uses the `Intl.Collator` API but with some differences in handling missing values and edge cases. It first checks if either of the input values is missing, and if so, returns a specific value (-1 or 1). However, for non-null values, it calls the `compare()` method on the `Collator` object to perform the comparison. 3. **Native String Comparison**: This option uses the built-in string comparison operator (`===`) to compare strings. **Pros and Cons** * **`agGridComparator()`**: Pros: handles edge cases like null or missing values, uses the `Intl.Collator` API for accurate comparisons. Cons: may have performance overhead due to the API call. * **`collatorCompare()`**: Pros: also handles edge cases, but with some differences in behavior compared to `agGridComparator()`. Cons: similar performance overhead as `agGridComparator()`. * **Native String Comparison**: Pros: fast and lightweight, no performance overhead. Cons: may not handle edge cases correctly. **Library and Purpose** The `Intl.Collator` API is a built-in JavaScript library that provides Unicode-aware string comparison functionality. It's designed to handle language-specific formatting, accentuation, and other complexities of human language. **Special JS Features or Syntax** This benchmark uses some special features in JavaScript: * The `Intl` namespace, which provides access to the Internationalization API. * The `Collator` class, which is used for comparing strings according to locale rules. * The `localeCompare()` method, which is a specialized version of the `compare()` method that takes into account language-specific formatting and accentuation. **Other Alternatives** If you wanted to use alternative approaches to compare strings in JavaScript, you could consider using: * The `String.prototype.localeCompare()` method (similar to `collatorCompare()`, but without the `Intl` namespace). * A custom implementation of string comparison using bitwise operators or regular expressions. * Other libraries like Moment.js for date and time comparisons. However, these alternatives may not provide the same level of accuracy or handling of edge cases as the `agGridComparator()` or `collatorCompare()` functions.
Related benchmarks:
parseInt vs Number vs implicit conversion
Implicit vs parseInt vs Number string to num
Implicit vs parseFloat vs Number string to num
Implicit vs parseInt vs Number string to num vs pipe
ParseInt vs conditional ~~
Comments
Confirm delete:
Do you really want to delete benchmark?