Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.SortBy vs Sort
(version: 0)
lodash sortby equivalent in Javascript
Comparing performance of:
Lodash vs Javascript
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js</script>
Script Preparation code:
var arr = [ {name: "test", age: 20}, {name: "best", age: 30}, {name: "blah", age: 50}, {name: "js", age: 10}, {name: "style", age: 29}, ];
Tests:
Lodash
_.sortBy(arr, 'age');
Javascript
var sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0); }; arr.sort(sortBy('age'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Javascript
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
1287940.8 Ops/sec
Javascript
6367210.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark compares two approaches for sorting an array in JavaScript: 1. **_.sortBy(arr, 'age')**: This uses the Lodash library to sort the array by the 'age' property. 2. **`var sortBy = (key) => {\r\n return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0);\r\n};\r\n\r\narr.sort(sortBy('age'));`**: This is a custom implementation of the sorting function using JavaScript's built-in `sort()` method. **Comparison Options** The benchmark compares two options: * **Lodash**: Uses the `_sortBy()` function from the Lodash library to sort the array. * **JavaScript Custom Implementation**: Uses a custom sorting function implemented in pure JavaScript. **Pros and Cons of Each Approach** **Lodash:** Pros: * Leverages optimized and tested code from a reputable library (Lodash). * Typically results in better performance due to the library's specialization in this area. Cons: * Adds an additional dependency to the benchmark. * May not be as flexible or customizable as a custom implementation. **JavaScript Custom Implementation:** Pros: * No external dependencies, making it more self-contained and easier to maintain. * Can be highly customized for specific use cases. Cons: * Requires manual implementation of the sorting algorithm, which may lead to errors or inefficiencies if not done correctly. * May require additional optimization or testing to achieve comparable performance to Lodash. **Library Usage (Lodash)** The benchmark uses the `_sortBy()` function from Lodash library. This function is designed to sort an array based on a specified key. It provides an efficient and stable way to perform sorting, which is beneficial in many cases. **Special JS Feature/Syntax (None)** This benchmark does not utilize any special JavaScript features or syntax, such as async/await, Promises, or modern language constructs like classes or modules. **Alternative Implementations** Other approaches for implementing the sorting function could include: * Using a different library or framework that provides a similar sorting functionality. * Implementing the sorting algorithm using a different data structure, such as a heap or a balanced binary search tree. * Utilizing a Just-In-Time (JIT) compiler or other performance optimization techniques to improve the custom implementation. Keep in mind that each approach has its own trade-offs and considerations. The benchmark's goal is to provide a fair comparison between these two options, allowing users to determine which one better suits their needs.
Related benchmarks:
compare _.SortBy vs Sort
compare _.SortBy vs Sort 1234
sortby vs orderby
lodash vs es6 in sort method
Comments
Confirm delete:
Do you really want to delete benchmark?