Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sortBy vs sort
(version: 0)
Comparing performance of:
sortBy string key vs sortBy func vs sort with 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.15/lodash.min.js'></script>
Script Preparation code:
var arr = Array(1000).fill().map(() => Math.random()).map(value => ({ value }));
Tests:
sortBy string key
_.sortBy(arr, "value");
sortBy func
_.sortBy(arr, x => x.value);
sort with comparator
arr.slice().sort((a, b) => a.value - b.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
sortBy string key
sortBy func
sort with comparator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sortBy string key
2854.3 Ops/sec
sortBy func
2718.7 Ops/sec
sort with comparator
7668.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents two benchmarks: `sortBy vs sort` and three individual test cases within it. **Benchmark Definition:** The first benchmark, `sortBy vs sort`, is comparing two approaches to sorting an array: 1. **Lodash's `sortBy` function**: a utility function that sorts the array based on a specified key or a custom comparison function. 2. **Native JavaScript's `sort` method with a comparator**: using the `sort` method directly, which requires providing a comparator function to compare elements. **Options Compared:** The two approaches are compared in terms of performance, and the benchmark is designed to measure their execution speed on different platforms (e.g., Firefox 121). **Pros and Cons:** 1. **Lodash's `sortBy` function**: * Pros: + Provides a convenient and flexible way to sort arrays based on a key or custom comparison. + Handles edge cases, such as null or undefined values. * Cons: + Adds additional overhead due to the library's functionality. 2. **Native JavaScript's `sort` method with a comparator**: * Pros: + Highly optimized and efficient for most use cases. + Only requires a single line of code. * Cons: + Requires manually implementing a comparison function, which can be error-prone. + May not handle edge cases as well as Lodash's `sortBy`. **Individual Test Cases:** 1. **`_.sortBy(arr, "value")` (Lodash)** * Purpose: Tests the performance of `sortBy` with a string key. * Library Used: Lodash.js 2. **`_.sortBy(arr, x => x.value)` (Lodash)** * Purpose: Tests the performance of `sortBy` with a custom comparison function. * Library Used: Lodash.js 3. **`arr.slice().sort((a, b) => a.value - b.value)` (Native JavaScript)** * Purpose: Tests the performance of sorting an array using the native `sort` method with a comparator. **Special JS Features/Syntax:** There is no special JavaScript feature or syntax being tested in these benchmarks. The focus is on comparing the performance of two approaches to sorting arrays. **Alternatives:** Other alternatives for sorting arrays include: 1. **Array.prototype.sort()**: similar to the native `sort` method with a comparator. 2. **Array.prototype.map().sort()**: transforms the array using `map`, and then sorts it. 3. **Libraries like moment.js, date-fns, or others**: specialized libraries for handling specific types of data (e.g., dates). Keep in mind that this is not an exhaustive list, and other approaches may exist depending on the specific requirements and constraints of your use case. Overall, these benchmarks provide a useful insight into the performance trade-offs between using Lodash's `sortBy` function versus native JavaScript's `sort` method with a comparator.
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
lodash sortBy vs native sortBy 4.17.21
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?