Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
gigi becal2i's test
(version: 0)
Comparing performance of:
_.sortBy vs sortByKey vs sortByKeyOriginal
Created:
3 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 getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for (var i = 0; i < 100000; i++) { arr.push({ value: getRandomInt(100) }); }
Tests:
_.sortBy
_.sortBy(arr,"value");
sortByKey
arr.sort((a,b) => a.value > b.value);
sortByKeyOriginal
arr.sort((a,b) => (a.value > b.value) ? 1 : (a.value < b.value) ? -1 : 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.sortBy
sortByKey
sortByKeyOriginal
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested, compared, and some pros/cons of each approach. **Benchmark Definition** The benchmark measures the performance of sorting an array of objects using three different methods: 1. `_.sortBy(arr, "value")`: Uses the Lodash library to perform a stable sort on the array based on the value property of each object. 2. `arr.sort((a, b) => a.value > b.value)`: Performs a custom comparison function-based sort on the array. 3. `arr.sort((a, b) => (a.value > b.value) ? 1 : (a.value < b.value) ? -1 : 0)`: Similar to the second approach, but uses a more explicit comparison function. **Options Compared** The benchmark compares three sorting algorithms: * **Lodash's `sortBy` method**: A popular library for functional programming tasks, providing a concise and efficient way to sort arrays. * **Custom comparison function-based sort**: A standard JavaScript method for sorting arrays using a callback function to compare elements. * **Explicit comparison function-based sort**: Similar to the previous approach, but uses a more explicit and verbose comparison function. **Pros/Cons of each approach** 1. **Lodash's `sortBy` method**: * Pros: Concise, efficient, and stable sorting algorithm, providing a high-level abstraction for common use cases. * Cons: Requires an additional library dependency (Lodash), may not be suitable for all edge cases or extreme performance requirements. 2. **Custom comparison function-based sort**: * Pros: Standard JavaScript method, easy to implement, and flexible for custom sorting scenarios. * Cons: Can lead to less readable code, and the number of comparisons required can grow exponentially with large datasets. 3. **Explicit comparison function-based sort**: * Pros: Provides a clear and explicit way to compare elements, making it easier to understand and debug the sorting process. * Cons: More verbose than the standard JavaScript method, which may lead to decreased readability. **Library - Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, object transformation, and more. The `sortBy` function in this benchmark uses Lodash's implementation of stable sorting, which ensures that equal elements remain in their original order during the sorting process. **Special JS Feature - Arrow Functions** The custom comparison function-based sort uses arrow functions (`(a, b) => a.value > b.value`) to define the comparison logic. Arrow functions are a concise way to define small, single-expression functions in JavaScript and provide several benefits, including: * **Less verbose**: Arrow functions eliminate the need for explicit `function` keywords and return statements. * **Implied scope**: Arrow functions inherit the scope of their surrounding function, eliminating the need for `this` keyword or additional binding. * **Readability**: Arrow functions can improve code readability by clearly indicating the intent behind a small function. Overall, this benchmark provides valuable insights into the performance differences between three distinct sorting algorithms, allowing developers to choose the most suitable approach based on their specific requirements and constraints.
Related benchmarks:
Labels
Lodash max vs JS Math.max (2022)
_.max vs Math.max
gigi becali's test
Comments
Confirm delete:
Do you really want to delete benchmark?