Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test custom order vs lodash
(version: 0)
test
Comparing performance of:
_.orderBy vs custom
Created:
2 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)); } function getValueAt(object, path) { const pathParts = path.split('.'); return pathParts.reduce((agg, item) => agg && agg[item], object); } function orderBy(data, sortKey, sortValue) { for (let i = 0; i < sortKey.length; i++) { data.sort((a, b) => { if (sortValue[i] === 'asc') { return getValueAt(a, sortKey[i]) > getValueAt(b, sortKey[i]) ? 1 : 0; } if (sortValue[i] === 'desc') { return getValueAt(b, sortKey[i]) < getValueAt(a, sortKey[i]) ? -1 : 0; } }); } return data; } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.orderBy
_.orderBy(arr,"value", "desc");
custom
orderBy(arr, ["value"], ["desc"]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.orderBy
custom
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark consists of two test cases: 1. `_.orderBy(arr, "value", "desc");`: This line uses the Lodash library to sort an array (`arr`) in descending order based on a specific key (`"value"`). 2. `orderBy(arr, ["value"], ["desc"]);`: This is a custom implementation of the same sorting function. **Options being compared** The two options being compared are: 1. **Lodash library**: The first test case uses the Lodash library to sort the array. 2. **Custom implementation**: The second test case implements the sorting function from scratch. **Pros and Cons of each approach** **Lodash library:** Pros: * Familiarity and reusability: Many developers are already familiar with Lodash and its utilities, making it easier to reuse and maintain. * Performance: Lodash is optimized for performance and provides a stable way to perform common tasks like sorting. Cons: * External dependency: The benchmark has to load the Lodash library, which may add overhead due to HTTP requests and loading times. * Dependence on external library: If the Lodash library changes or breaks, the test case will need to be updated. **Custom implementation** Pros: * Control and customization: By implementing the sorting function from scratch, developers can customize the behavior and performance of the benchmark to suit their specific needs. * No external dependency: There is no overhead due to loading an external library. Cons: * Complexity: Implementing a custom sorting function can add complexity and increase maintenance costs. * Potential for bugs: Without proper testing, the custom implementation may contain bugs or inefficiencies. **Library and its purpose** Lodash (now known as Locomotive) is a utility-first JavaScript library that provides a collection of functions for common tasks like array manipulation, object manipulation, and more. In this benchmark, Lodash is used to provide the `orderBy` function, which sorts an array based on a specific key. **Special JS feature or syntax** There are no special features or syntax in this benchmark. The code uses standard JavaScript features like functions, arrays, and object methods. **Other alternatives** If you wanted to modify the custom implementation of `orderBy`, you could consider using other libraries like: * **lodash-es**: A more modern version of Lodash with improved performance and features. * **lodash-clamp**: Another utility library that provides similar functionality to Lodash. * **custom sorting algorithms**: You could use a different sorting algorithm, like QuickSort or Merge Sort, which might be optimized for specific use cases. Keep in mind that using external libraries can introduce dependencies and overhead, while custom implementations require more effort but provide full control.
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
Lodash orderBy vs array.prototype.sort fork
Dashi's tests
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?