Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[js] localeCompare sort vs normal sort vs lodash orderBy vs sort comparator w/ lorem (n=57200) Copy Copy Copy
(version: 0)
Comparing performance of:
localeCompare vs lodash order vs sort comparator
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function shuffle(array) { var i = array.length, j = 0, temp; while (i--) { j = Math.floor(Math.random() * (i + 1)); temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } var words = [ "wh@}U;8(9%hID#E;y*FQ*\Gf=DQ]C", "w]=R4nO[Pqc,;c8GFgAu[m^$l.V", "kbxmGZUcij0_DJ,;PZ~=#vPJA*hHG", ":D2dqJ-h/F;vh?!=}4]pKk<Qz0L=z", "oB>.3IL2TFhMqUOp!MzVSoG0!4uw", "@Je$BH@ox,Wc])~.Ed]AS}Ox&AgiY", "rH@;R:}.ZaB0aKB\QR7\h\^8y$", "LtO+CzRVp\Mp;vm87f3)P+|MxNKh", "(a0?*16Wa1bt9{9g>M8~^Y)ZkLO19", "2n.[?5JghKW$}jh=;b6DbWiSe6l4;", "4NpZ-4uIA^!VmN+\t0wh95DG=VC0", "4#DiMlrOjL#cxGU!rWr<VM8iRYR70D", "5|O6Vq+%aE]rsVUYEuGceyoaD$v&#", "#\ISPh1ra&&>9/NKlr3.Ez<P^I4pY", "M!Ery#SkPM5^A/J|VcI6WL\>&i=K", "nBx5kFrOB7^Lo)1Bd5>4H5T2t(", "A{q+L|LKxb-mQte-C\gjne4dC?#O_", "I):fgN?r?p&g6j7-j,,WOX?G+0#w~b", "c3w3%/q#drU!^;EP2AP.0@;8o.AV}", "EMP)(T;DUUdt|(7|50KWafo};-IW", "q]\\N}Mzq{GbZQH!6t^J%-ZX1a2*dd", "a|u.t3nq@rk\|M+I,Z5i_7\%VH-kU", "Yzn&}SET6ocLsQj5P/L_M!WPbc~)-", "p&)]$l2S&/Nlfv(D|\dRUT(Zu/9#:", "VOIb^R;vw;rMYG84n7.&kcj+oBUJ.]", "I^:@O0un-IPTb*xcd##o\cYusqi^#;", "FT<woNY|hK|#{Xafm5v\=OX9yv|%aC", "-5l)]&z~bR2zjSGPBJaJ9xR=TC&5mE", "=.Eq1W>{0~Mbm/oJlZOr@bPS2T&KO", "q>BN^_SvJd>+b:]4s6i3)^GsaD{<s" ]; var words1 = []; var n = []; for (i = 0; i < 4; i++) words.push(...words); for (i = 0; i < words.length; i++) n.push(i); var ranNums = shuffle(n); for (i = 0; i < words.length; i++) words1.push(words[ranNums[i]]); console.log('ORIGINAL', words1);
Tests:
localeCompare
words1.sort((a,b) => a.localeCompare(b));
lodash order
_.orderBy(words1,model => model,'asc');
sort comparator
words1.sort((a,b) => { if (a.toLowerCase() < b.toLowerCase()) { return -1; } if (a.toLowerCase() > b.toLowerCase()) { return 1; } return 0; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
localeCompare
lodash order
sort comparator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
localeCompare
220207.2 Ops/sec
lodash order
33518.3 Ops/sec
sort comparator
55664.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark measures the performance of three sorting algorithms: 1. Native JavaScript `localeCompare` (also known as "natural sort") 2. Lodash `orderBy` 3. Custom sorting with a comparator function The test case consists of 57,200 strings (words) in an array, which are randomly shuffled and then sorted using each of the three methods. **Options Compared** 1. **Native JavaScript `localeCompare`**: This method uses a natural sorting algorithm that sorts strings based on their characters' ASCII values, taking into account punctuation and word boundaries. 2. **Lodash `orderBy`**: This method uses a stable sort algorithm to arrange elements in a specific order based on a provided model function. 3. **Custom sorting with a comparator function**: This approach allows for more control over the sorting process by providing a custom comparison function. **Pros and Cons of Each Approach** 1. **Native JavaScript `localeCompare`**: * Pros: + Fast, as it's a built-in method optimized for performance. + Simple to use, with minimal code required. * Cons: + May not be suitable for non-ASCII characters or complex sorting scenarios. 2. **Lodash `orderBy`**: * Pros: + Flexible and customizable through the model function. + Handles edge cases like duplicate elements and non-numeric values. * Cons: + Requires an additional library (Lodash). + May have performance overhead due to its stability and flexibility features. 3. **Custom sorting with a comparator function**: * Pros: + Highly customizable, allowing for fine-grained control over the sorting process. + Suitable for complex sorting scenarios or non-ASCII characters. * Cons: + Requires more code and development effort to implement correctly. **Other Considerations** 1. **Stability**: Lodash `orderBy` is a stable sort algorithm, whereas native JavaScript `localeCompare` and custom comparator functions may not be stable (i.e., the order of equal elements is preserved). 2. **Performance**: Native JavaScript `localeCompare` is likely to be the fastest option due to its built-in optimization. Custom comparator functions can be faster or slower depending on their implementation. 3. **Maintenance**: Lodash `orderBy` requires an additional library, which may require more maintenance and updates compared to native JavaScript methods. By comparing these three options, the benchmark provides insights into the performance characteristics of each sorting algorithm in different scenarios.
Related benchmarks:
[js] localeCompare sort vs normal sort vs lodash orderBy vs sort comparator w/ lorem (n=57200)
[js] localeCompare sort vs normal sort vs lodash orderBy vs sort comparator w/ lorem (n=57200) Copy
[js] localeCompare sort vs normal sort vs lodash orderBy vs sort comparator w/ lorem (n=57200) Copy Copy
[js] localeCompare sort vs normal sort vs lodash orderBy vs sort comparator w/ lorem (n=57200) Copy Copy Copy Copy
Comments
Confirm delete:
Do you really want to delete benchmark?