Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native sort of objects by localeCompare vs lodash _.sortBy
(version: 0)
comparing native sort of objects by localeCompare vs lodash _.sortBy
Comparing performance of:
native sort of objects by localeCompare vs lodash _.orderBy
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
Script Preparation code:
var users = [ { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 } ]
Tests:
native sort of objects by localeCompare
users.sort((a,b) => a.user.localeCompare(b.user));
lodash _.orderBy
_.sortBy(users, 'user', 'asc')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native sort of objects by localeCompare
lodash _.orderBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
native sort of objects by localeCompare
5566625.5 Ops/sec
lodash _.orderBy
2707280.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Overview** The provided JSON represents a benchmark test on MeasureThat.net, which compares the performance of two approaches for sorting objects by localeCompare: native sort using `localeCompare` and `lodash _.sortBy`. The benchmark also includes a second test case that uses `lodash _.orderBy`, but this will be discussed separately. **Options Compared** The two options compared in this benchmark are: 1. **Native Sort using `localeCompare`**: This approach uses the built-in `localeCompare` method to compare strings. 2. **Lodash _.sortBy**: This approach uses the popular JavaScript library Lodash's `sortBy` function to sort objects. **Pros and Cons of Each Approach** ### Native Sort using `localeCompare` Pros: * Lightweight, no external dependencies * Fast, since it's implemented in native code * Well-supported by most browsers Cons: * Can be slower for large datasets due to the overhead of string comparisons * May not work as expected with certain locale settings (e.g., accents or non-ASCII characters) ### Lodash _.sortBy Pros: * Robust, well-tested, and widely adopted * Flexible, allows for custom sorting keys and behaviors * Works well with large datasets Cons: * Adds external dependency (Lodash), which may increase load time * May have slower performance compared to native sort due to the overhead of function calls **Library: Lodash** Lodash is a popular JavaScript library that provides a wide range of utility functions for tasks like sorting, mapping, and more. The `sortBy` function takes three arguments: an array of objects to be sorted, a key function, and a sorting order (in this case, ascending). **Special JS Feature/ Syntax: localeCompare** The `localeCompare` method is a built-in JavaScript function that compares two strings according to the Unicode Standard for comparing strings. It's used here to compare string values in the objects being sorted. Note that `localeCompare` takes into account various factors like accent marks, non-ASCII characters, and language-specific sorting rules. **Alternatives** If you're interested in exploring alternative approaches, consider: * Other JavaScript libraries like Moment.js for date-based sorting or Ramda for functional programming. * Built-in methods like `Array.prototype.sort()` with custom sorting keys or behaviors. However, keep in mind that these alternatives might introduce additional dependencies, complexity, or performance overhead compared to the native sort using `localeCompare`.
Related benchmarks:
native sort of objects by localeCompare vs lodash _.orderBy swedish
native sort of objects by linus
a native sort of objects by localeCompare vs lodash _.orderBy
native sort of objects by localeCompare vs lodash _.sortBy []
Comments
Confirm delete:
Do you really want to delete benchmark?