Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash differenceby vs sort2
(version: 0)
Comparing performance of:
sort vs differenceBy
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
Script Preparation code:
var values = Array(10000).fill(null).map((x, a) => ({number: a})); var values2 = Array(10000).fill(null).map((x, a) => ({number: 10000-a}));
Tests:
sort
const result = _.isEqual(values.sort(), values2.sort()) console.log(result)
differenceBy
const result = _.differenceBy(values, values2, _.isEqual)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sort
differenceBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 YaBrowser/25.2.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sort
1560.1 Ops/sec
differenceBy
2849.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Overview** The benchmark compares the performance of two functions from the Lodash library: `_.isEqual` (which checks for deep equality) and `_.differenceBy`. The goal is to determine which function is faster when applied to two large arrays with unique elements. **Options Compared** There are two options being compared: 1. **_.isEqual(values.sort(), values2.sort())**: This option sorts both arrays before comparing them using the `.isEqual` function. Sorting has a time complexity of O(n log n), so this approach may be slower for large datasets. 2. **_.differenceBy(values, values2, _.isEqual)**: This option uses the `_.differenceBy` function to remove elements from the first array (`values`) that exist in the second array (`values2`). The callback function provided to `_.differenceBy` is set to `_.isEqual`, which checks for deep equality. This approach may be faster than sorting because it avoids the overhead of sorting. **Pros and Cons** * **Sorting**: Pros: allows for a simple comparison of two arrays in sorted order. Cons: can be slow for large datasets due to its time complexity (O(n log n)). * **_.differenceBy**: Pros: potentially faster than sorting, as it avoids the overhead of sorting. Cons: requires more complex logic and may not always produce the expected results if the callback function is not correctly implemented. **Library and Purpose** The Lodash library provides a set of utility functions for functional programming in JavaScript. The `_.isEqual` function checks whether two values are equal, considering both value and reference equality. In this benchmark, it's used to compare arrays after sorting or removing elements using `_.differenceBy`. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Other Alternatives** If the goal of the benchmark were different, other approaches might be explored: * Instead of sorting both arrays, one array could be sorted and the other left unsorted to see if there's a significant difference. * Alternative comparison functions could be used instead of `_.isEqual`, such as `_.equal` or `_.deepEqual`. * Different data structures, like linked lists or trees, could be used instead of arrays. Overall, this benchmark provides a useful comparison between two common array manipulation operations in JavaScript: sorting and using `_.differenceBy`.
Related benchmarks:
sortby vs orderby
Sort lodash vs native
uniqBy performance lodash vs native
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?