Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
agGrid vs Collator
(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) { 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
5728406.0 Ops/sec
collatorCompare
2824302.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and some pros/cons of each approach. **Benchmark Definition JSON** The benchmark is comparing two JavaScript functions: 1. `agGridComparator` 2. `collatorCompare` Both functions take two arguments (`valueA` and `valueB`) and return an integer indicating their comparison (0 for equal, 1 if `valueA` is less than `valueB`, -1 otherwise). **Options Compared** The options being compared are: * `numeric`: set to `true` in the benchmark definition * `accentedCompare`: a boolean flag used in the `agGridComparator` function **What's Being Tested?** The tests are measuring how efficiently each function compares two strings, taking into account potential differences in formatting (e.g., numbers vs. localized strings). The primary focus is on performance, as indicated by the number of executions per second. **AgGridComparator** This function uses a combination of techniques to compare values: 1. **Number wrapping**: If either value can be converted to a number using `toNumber()`, it's done so to ensure accurate comparisons. 2. **LocaleCompare**: For strings, if `accentedCompare` is false or localeCompare throws an error (e.g., due to unsupported locale), it falls back to a quick comparison using `localeCompare()`. Pros: * Handles numbers and localized strings accurately * Uses localeCompare for precise string comparisons Cons: * May have performance overhead due to localeCompare's potential errors * May not be suitable for all edge cases (e.g., Chinese characters) **CollatorCompare** This function uses the `Intl.Collator` API, which is part of the modern JavaScript standard library. Pros: * Provides accurate comparisons for localized strings and numbers * Fast and efficient Cons: * Requires support for the `Intl.Collator` API in browsers (some older versions may not support it) * May have limited internationalization capabilities compared to localeCompare **Other Considerations** When testing these functions, you'll want to consider factors like: * Performance: How many executions per second can each function achieve? * Accuracy: Are the comparisons accurate for various edge cases? * Browser compatibility: Does each function work across different browsers and versions? **Alternatives** If you're interested in exploring other options or alternatives, some possible approaches include: 1. **Standardize localeCompare**: Consider using a standardized approach like `localeCompare()` for both numbers and strings. 2. **Use a third-party library**: There are several libraries available that provide more advanced internationalization capabilities, such as ` moment.js` or ` i18n-js`. 3. **Optimize for performance**: If you're concerned about performance, consider optimizing the functions for specific use cases or edge cases. Keep in mind that this is just a high-level overview of the benchmark and its components. To gain deeper insights, I recommend exploring the provided benchmark definition code and experimenting with different variations to understand their impact on performance and accuracy.
Related benchmarks:
parseInt vs Number addition
Number constructor vs unary plus vs parseInt
parseInt vs Number vs implicit conversion
parseInt vs Number vs Plus addition
Number vs + vs parseFloat + eval
Comments
Confirm delete:
Do you really want to delete benchmark?