Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
collatorCompare() vs collatorCompare2() with isBlank
(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 hasUnknownProperty(val) { return ( typeof val === 'object' && val !== null && typeof (val).unknownProperty === 'function' ); } function isEmpty(obj) { if (obj === null || obj === undefined) { return true; } if (!hasUnknownProperty(obj) && typeof(obj).size === 'number') { return !(obj).size; } if (typeof obj === 'object') { let size = get(obj, 'size'); if (typeof size === 'number') { return !size; } let length = get(obj, 'length'); if (typeof length === 'number') { return !length; } } if (typeof(obj).length === 'number' && typeof obj !== 'function') { return !(obj).length; } return false; } function isBlank(obj) { return isEmpty(obj) || (typeof obj === 'string' && /\S/.test(obj) === false); } function collatorCompare2(valueA, valueB) { if (valueA === valueB) { return 0 } else if (isBlank(valueA)) { return -1 } else if (isBlank(valueB)) { 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()
1826193.6 Ops/sec
collatorCompare()
2526319.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark defines two functions: `collatorCompare()` and `collatorCompare2()`. The goal of the benchmark is to compare these two functions for collation purposes. A collator is a service that compares strings in a culturally relevant way. **Options Compared** Two options are compared: 1. **`collatorCompare()`**: This function uses the `Intl.Collator()` API to perform collation. It checks if either input value is missing, and if so, returns a negative or positive value accordingly. If both values are present, it calls the `compare()` method of the collator object. 2. **`collatorCompare2()`**: This function also uses the `Intl.Collator()` API to perform collation. However, it simplifies the comparison by checking if either input value is blank (empty or contains only whitespace) before calling the `compare()` method. **Pros and Cons of Each Approach** 1. **`collatorCompare()`**: * Pros: More comprehensive and accurate, as it checks for missing values. * Cons: May be slower due to the additional checks, and may require more system resources. 2. **`collatorCompare2()`**: * Pros: Faster and more lightweight, as it skips unnecessary checks. * Cons: May produce incorrect results if either input value is blank, depending on the locale's collation rules. **Other Considerations** * The benchmark uses a string literal `FOO BAR` and its lowercase version `foo bar` to test the collation behavior. This ensures that the function handles case-insensitive comparisons. * The `isBlank()` function checks if an object is empty or contains only whitespace characters. This is used in `collatorCompare2()` to simplify the comparison. * The `hasUnknownProperty()` function checks if an object has a property with a specific name (`unknownProperty`). This is not directly related to the collation functionality but may be relevant for other parts of the benchmark. **Library: Intl.Collator** The `Intl.Collator` API is part of the ECMAScript Internationalization API (ECMA-402). It provides a way to perform collation and other cultural tasks, such as sorting and comparing strings in a locale-specific manner. The library is used by both `collatorCompare()` and `collatorCompare2()`. **Special JS Feature: String.prototype.trim()** The `trim()` method is used implicitly in the `isBlank()` function to remove whitespace characters from the input string. This is not a special JavaScript feature, but rather a built-in method of the `String` prototype. **Alternatives** If you want to implement a custom collation algorithm without relying on the `Intl.Collator` API, you could consider using a combination of character comparison and locale-specific rules. However, this would likely require more manual effort and may not provide the same level of accuracy and flexibility as the built-in `Intl.Collator` API. Keep in mind that implementing a custom collation algorithm can be complex and may require knowledge of the specific locale's collation rules and cultural nuances.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty 25
undefined vs. typeof vs. in vs. hasOwnProperty 30
undefined vs. typeof vs. in vs. hasOwnProperty 32
?. operator vs. getProperty
undefined vs null vs bool vs isInteger check
Comments
Confirm delete:
Do you really want to delete benchmark?