Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native javascript vs lodash sort
(version: 0)
Comparing performance of:
lowdash vs JS
Created:
6 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:
var test = [{"value":"97535676-acbb-41f4-b8ea-91e8e5e3fc0d","label":"Overview"},{"value":"f1e5845d-b5a4-4725-9813-f601eaed6826","label":"test 1"},{"value":"056d7e5a-b947-4c10-8b38-13416deebe4a","label":"Announcements"},{"value":"18149d83-8191-4c60-afca-d976f6f63ca5","label":"Browse buttons"},{"value":"20b145fa-14ae-457c-9fec-04fdd48ddba2","label":"ztest"},{"value":"00142552-9bf6-49a8-829d-152d8448ac5f","label":"Watson News"},{"value":"578695c6-4e4c-4c8d-ba8a-2ede7b6abd19","label":"Filtered"},{"value":"6b6fef15-a13e-423b-875d-7700737dccf4","label":"Curated"}]
Tests:
lowdash
_.sortBy(test, [ name => { const priority = ['overview']; if (_.includes(priority, name)) { return _.indexOf(priority, name); } else { return name.length; } }, name => name, ]);
JS
test.sort(function(a, b) { const valueA = a.label.toUpperCase(); const valueB = b.label.toUpperCase(); if (valueB === 'OVERVIEW') { return 1; } else if (valueA === 'OVERVIEW') { return -1; } return valueA < valueB ? -1 : valueA > valueB ? 1 : 0; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lowdash
JS
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
gemma2:9b
, generated one year ago):
This benchmark compares two approaches to sorting an array of objects: **Option 1: Using Lodash's `_.sortBy()` function.** * **Code:** The provided code utilizes Lodash's `_.sortBy()` function with a custom comparison function. This function prioritizes objects with the label "Overview" at the beginning of the sorted array, then sorts remaining objects by their label length. * **Pros:** * Lodash is a widely used library with optimized functions for common tasks like sorting. Using it can often lead to more concise and readable code. * The custom comparison function allows for fine-grained control over the sorting order. * **Cons:** * Adds an external dependency (Lodash) to your project. This might increase bundle size and loading time, especially if Lodash is not used elsewhere in your application. **Option 2: Native JavaScript `sort()` method.** * **Code:** The code implements a custom comparison function directly within the `sort()` method. It prioritizes objects with the label "Overview" at the beginning, then sorts the remaining objects alphabetically by their label. * **Pros:** * Does not require any external libraries. Your project remains self-contained. * Native functions are generally well-optimized and part of the JavaScript standard. * **Cons:** * Can be more verbose than using Lodash's `_.sortBy()`. **Other Considerations:** * **Performance:** The benchmark results show that the native JavaScript implementation (`JS` test) is significantly faster (around 7 times faster) than using Lodash's `_.sortBy()` function (`lowdash` test). This suggests that in this specific case, the overhead of using Lodash might outweigh its benefits. * **Readability:** Both implementations are relatively readable. However, Lodash's `_.sortBy()` might be considered slightly more concise for simple sorting tasks. **Alternatives:** In addition to these two options, there are other libraries and methods you could explore for sorting arrays in JavaScript: * **Immer:** Offers an immutable data structure approach that can lead to performance improvements and easier debugging. * **Array.prototype.sort() with different comparison functions:** Experimenting with various comparison functions within `sort()` can provide more flexibility depending on your specific needs.
Related benchmarks:
_.SortBy vs Sort
compare _.SortBy vs Sort 1234
lodash vs es6 in sort method
_Vs_Native
order desc with lodash orderBy vs es6 sort method
Comments
Confirm delete:
Do you really want to delete benchmark?