Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
orderBy vs Native sort native dynamic compare
(version: 9)
compare lodash order by vs a custom native sort function
Comparing performance of:
lodash orderBy vs Custom NativeSort
Created:
6 years ago
by:
Registered User
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:
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:
lodash orderBy
_.orderBy(arr,"value","asc");
Custom NativeSort
function get(selector, obj) { return selector.split('.').reduce((acc, part) => { // O(n2) when querying for an array (e.g. items[0].name) // Likely, the object depth will be reasonable enough that performance is not a concern return acc[part]; }, obj); } function compareValues(key, order) { return (a, b) => { if (!key) { return 0; } const leftValue = get(key, a); const rightValue = get(key, b); const left = (typeof leftValue === 'string') ? leftValue.toUpperCase() : leftValue; const right = (typeof rightValue === 'string') ? rightValue.toUpperCase() : rightValue; const comparison = (left > right) ? 1 : -1; return ( (order === 'desc') ? (comparison * -1) : comparison ); }; } arr.concat().sort(compareValues("value"))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash orderBy
Custom NativeSort
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark measures the performance difference between using Lodash's `orderBy` function and a custom native sorting approach in JavaScript. The test creates an array of 100,000 objects with random integer values and compares the execution times of both methods. **Options Compared** Two options are being compared: 1. **Lodash `orderBy`**: This is a third-party library that provides various utility functions, including sorting. 2. **Custom Native Sort**: This is a custom implementation of the sorting algorithm using JavaScript's built-in functions. **Pros and Cons of Each Approach** ### Lodash `orderBy` Pros: * Easy to use and well-documented * Handles edge cases and corner cases out of the box Cons: * Requires an external library (Lodash) to be included in the test environment * May have performance overhead due to the additional library load * Limited control over the sorting algorithm used by Lodash ### Custom Native Sort Pros: * Provides fine-grained control over the sorting algorithm * No dependencies on external libraries * Can potentially offer better performance due to optimized native code Cons: * Requires manual implementation of the sorting algorithm, which can be error-prone * May not handle edge cases or corner cases as well as Lodash's implementation **Library and Its Purpose** The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks such as: * Array manipulation (e.g., `orderBy`) * String manipulation (e.g., `upperCase`) * Object manipulation (e.g., `cloneDeep`) * Functional programming utilities (e.g., `map`, `reduce`) In this benchmark, Lodash's `orderBy` function is used to sort the array based on a specific key. **Special JS Feature or Syntax** The custom native sorting implementation uses some advanced JavaScript features: * **Reduce**: A method that applies a function to each element of an array and reduces it to a single output value. * **Split**: A string method that splits a string into an array of substrings based on a specified separator. * **Array methods**: The implementation uses various array methods such as `concat` and `sort` to manipulate the data. These features are part of the standard JavaScript language specification and are widely supported by modern browsers. **Other Alternatives** If you're interested in exploring other sorting algorithms or libraries, here are a few alternatives: * **Quicksort**: A popular sorting algorithm that is known for its average-case time complexity of O(n log n). * **Merge Sort**: Another efficient sorting algorithm with an average-case time complexity of O(n log n). * **Algorithms like Radix sort**: Specialized algorithms designed to perform well on specific data types, such as integers or strings. Keep in mind that the choice of sorting algorithm or library depends on the specific requirements and constraints of your project.
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
lodash sortBy vs native sortBy 4.17.21
Lodash sortBy vs orderBy performance
Lodash orderBy vs array.prototype.sort fork
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?