Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sortAlphaNumUsingRegex2() vs localeCompare('en', {numeric: true})
(version: 0)
Comparing performance of:
sortAlphaNum() 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); var reA = /[^a-zA-Z]/g; var reN = /[^0-9]/g; function sortAlphaNum(a, b) { var regExp = /([0-9]+)|([a-zA-Z]+)/g; var aParts = a.match(regExp); var bParts = b.match(regExp); var collator = new Intl.Collator('en', { numeric: true }); while (aParts.length > 0 && bParts.length > 0) { var aPart = aParts.shift(); var bPart = bParts.shift(); if (isNaN(aPart) && isNaN(bPart)) { if (aPart !== bPart) { return collator.compare(aPart, bPart); } } else { aPart = parseInt(aPart) || 0; bPart = parseInt(bPart) || 0; if (aPart !== bPart) { return aPart - bPart; } } } return aParts.length - bParts.length; }
Tests:
sortAlphaNum()
sortAlphaNum(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
sortAlphaNum()
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
sortAlphaNum()
261944.7 Ops/sec
Intl.Collator.compare()
5822163.0 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 considered. **Benchmark Definition** The website `MeasureThat.net` provides two test cases: 1. `sortAlphaNumUsingRegex2() vs localeCompare('en', {numeric: true})` 2. `collator.compare(a, b) === 0` Let's focus on the first test case: The benchmark compares two sorting functions: * `sortAlphaNumUsingRegex2()`: a custom implementation using regular expressions to sort strings with alphanumeric characters. * `localeCompare('en', {numeric: true})`: a built-in JavaScript function that sorts strings based on locale-specific rules, including numeric sorting. **Options Compared** The benchmark compares the two functions under different options: * The first test case uses a constant string `a = "FOO BAR";` and another string `b = "foo bar";`. These strings contain both alphabetical and numerical characters. * The second test case is not relevant to the current explanation, but it seems to be related to testing a collator function. **Pros and Cons of Each Approach** 1. `sortAlphaNumUsingRegex2()`: * Pros: + Can handle any type of alphanumeric character (e.g., Unicode characters). + Allows for fine-grained control over the sorting logic. * Cons: + More complex to implement, as it requires regular expressions and handling edge cases. + May not perform well on large datasets due to the overhead of string matching. 2. `localeCompare('en', {numeric: true})`: * Pros: + Efficient and optimized for performance by the JavaScript engine. + Handles most common use cases, including numeric sorting. * Cons: + Limited control over the sorting logic (e.g., no support for custom locale rules). + May not handle all types of alphanumeric characters correctly. **Library Used** The `localeCompare` function uses the Internationalization API (`Intl.Collator`), which is a built-in JavaScript library that provides locale-specific sorting and formatting capabilities. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax, but it relies on the built-in `Intl.Collator` API to perform locale-specific sorting.
Related benchmarks:
Intl.Collator.compare() vs localeCompare() no options sort
Number Conversion Speed
Format number | Regex vs Code V1.1
sortAlphaNumUsingRegex() vs localeCompare('en', {numeric: true})
Comments
Confirm delete:
Do you really want to delete benchmark?