Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Alphabetical sort native javascript vs lodash
(version: 0)
Comparing performance of:
lodash 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:
lodash
_.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
lodash
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
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark compares the performance of two approaches for sorting an array of objects: 1. Native JavaScript sort function 2. Lodash `sortBy` function **Test Cases** There are two test cases: 1. **Lodash**: The benchmark definition uses Lodash's `sortBy` function to sort the `test` array based on two criteria: * `name => _.includes(priority, name)`: If the `name` is in the `priority` array (which contains only "overview"), returns the index of the `name` in the `priority` array. Otherwise, returns the length of the `name`. * `name => name`: Sorts by the `label` property of each object. 2. **Native JavaScript**: The benchmark definition uses a custom sort function to sort the `test` array based on two criteria: * `a.label.toUpperCase()` and `b.label.toUpperCase()`: Compare the uppercase labels of each object. If one label is "OVERVIEW", returns -1 if it comes before another "OVERVIEW" label, 1 if it comes after, or 0 if they are equal. * A simple length comparison: Returns -1 if the first label has a shorter length, 1 if it's longer, or 0 if they're equal. **Library and Purpose** Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string manipulation, and more. The `sortBy` function in Lodash allows you to sort an array of objects based on one or multiple criteria. **Special JS Feature/Syntax** None mentioned in the benchmark definition. **Comparison of Approaches** Here's a brief analysis of each approach: 1. **Lodash `sortBy`**: This approach uses a stable sorting algorithm (Timsort, which is also used by JavaScript's built-in sort function) to sort the array. The use of Lodash provides a convenient and expressive way to specify the sorting criteria. * Pros: + Easy to read and maintain + Provides a stable sort * Cons: + Adds an external dependency (Lodash) 2. **Native JavaScript**: This approach uses a custom sort function that combines two simple comparisons. * Pros: + No external dependencies + Simple to understand * Cons: + The custom sorting logic can be error-prone and difficult to maintain **Other Alternatives** If you don't want to use Lodash, you could consider using other JavaScript libraries or frameworks that provide similar sorting functionality, such as: * Moment.js for date-based sorting * Angular's `orderBy` pipe for data binding and sorting * React's built-in `sort` function in the `lodash` module Alternatively, if you prefer not to use any external dependencies, you could implement your own sorting algorithm using JavaScript's built-in functions, such as `Array.prototype.sort()`. I hope this explanation helps!
Related benchmarks:
_.SortBy vs Sort
compare _.SortBy vs Sort 1234
sortby vs orderby
lodash vs es6 in sort method
order desc with lodash orderBy vs es6 sort method
Comments
Confirm delete:
Do you really want to delete benchmark?