Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
gigi becali's test
(version: 0)
Comparing performance of:
_.sortBy vs sortByKey
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
const sortByKey = (array, key) => arr.sort((a,b) => a[key] > b[key]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sortBy
sortByKey
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 considered. **Benchmark Definition** The website MeasureThat.net allows users to create microbenchmarks by providing JavaScript code that represents a specific task or operation. The provided benchmark definition consists of: * `Name`: A descriptive name for the benchmark. * `Description`: An optional description of the benchmark (not provided in this example). * `Script Preparation Code`: A JavaScript function that prepares the data needed for the benchmark. In this case, it generates an array with 100,000 objects, each containing a random value between 0 and 100. * `Html Preparation Code`: A snippet of HTML that includes a reference to the Lodash library (version 4.17.5), which provides functional programming utilities. **Individual Test Cases** The benchmark defines two individual test cases: 1. **_.sortBy(arr,"value")`**: This test case uses the `_.sortBy()` function from the Lodash library, which sorts an array in place based on a specified key. 2. **`const sortByKey = (array, key) => arr.sort((a,b) => a[key] > b[key])`**: This test case implements a custom sorting algorithm that sorts an array based on a specific key. **Comparison of Approaches** The two approaches are compared in terms of their performance: * **_.sortBy(arr,"value")`: Uses the Lodash library to perform a stable sort on the array. * **`const sortByKey = (array, key) => arr.sort((a,b) => a[key] > b[key])`**: Implements a custom sorting algorithm using the `sort()` method and a compare function. **Pros and Cons** * **_.sortBy(arr,"value")`**: + Pros: - Uses a well-maintained, optimized library. - Provides a stable sort, which is important for certain data structures (e.g., arrays with duplicate values). + Cons: - Requires an additional library dependency. * **`const sortByKey = (array, key) => arr.sort((a,b) => a[key] > b[key])`**: + Pros: - Does not require an additional library dependency. - Can be optimized for specific use cases (e.g., using `BigInt` for large numbers). + Cons: - Requires manual implementation of the sorting algorithm, which can lead to errors. **Library: Lodash** The Lodash library provides a comprehensive set of functional programming utilities, including `_.sortBy()`. This function is designed to perform stable sorts on arrays, ensuring that equal elements maintain their original order. **Special JS Features/Syntax (Not Applicable in this Example)** There are no special JavaScript features or syntax used in this benchmark. The code only utilizes standard ECMAScript syntax and libraries (Lodash). **Other Alternatives** For custom sorting algorithms like `const sortByKey = (array, key) => arr.sort((a,b) => a[key] > b[key])`, other alternatives include: * Using the `Array.prototype.sort()` method with a custom compare function. * Implementing a merge sort or quicksort algorithm. * Utilizing a library like [fast-sort](https://github.com/isaacs/fast-sort) for optimized sorting. Keep in mind that these alternatives may have different performance characteristics and trade-offs, depending on the specific use case and requirements.
Related benchmarks:
Labels
Lodash max vs JS Math.max (2022)
_.max vs Math.max
gigi becal2i's test
Comments
Confirm delete:
Do you really want to delete benchmark?