Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sort + indexOf vs intersection
(version: 4)
Comparing performance of:
intersection vs sort + indexOf
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.5/lodash.min.js'></script>
Script Preparation code:
var treeIds = Array.from({length: 100}).map((_, index) => `${index}`); var selectedIds = _.shuffle(treeIds).slice(0,20);
Tests:
intersection
const intersection = _.intersection(treeIds, selectedIds);
sort + indexOf
selectedIds.sort((i, j) => treeIds.indexOf(i) - treeIds.indexOf(j))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
intersection
sort + indexOf
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 benchmark and explore what's being tested. **Benchmark Overview** The benchmark measures the performance of two approaches to find common elements between two arrays: using `_.intersection` from Lodash or using the `sort` method with `indexOf`. **Options Being Compared** There are two options being compared: 1. **`.intersection()`**: This method returns a new array containing only the elements present in both input arrays. 2. **Sorting and then finding index of corresponding element (`sort + indexOf`)**: This approach sorts the first array, finds the index of each element in the second array, and then creates a new array with these indices. **Pros and Cons** 1. `.intersection()`: * Pros: efficient, concise, and easy to understand. * Cons: may not be suitable for very large datasets due to its iterative nature. 2. **Sorting and then finding index of corresponding element (`sort + indexOf`): * Pros: can handle larger datasets more efficiently since it only iterates over the sorted array once. However, it requires sorting, which can be slower for small arrays. * Cons: may not be suitable for smaller arrays due to its additional overhead (sorting). **Other Considerations** Both approaches have a time complexity of O(n log n) for large datasets. However, `.intersection()` might have an edge in terms of constant factors due to its optimized implementation. **Lodash Library and Its Purpose** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string handling, and more. In this benchmark, _.intersection() is used to find common elements between two arrays. **Special JS Feature or Syntax** There's no special feature or syntax being tested in this benchmark. The code is straightforward JavaScript, using standard library methods (Lodash) and built-in functions (`Array.prototype.indexOf()`). **Alternative Approaches** Some alternative approaches could be: 1. Using a different sorting algorithm (e.g., quicksort, mergesort). 2. Implementing a custom solution using bitwise operations or other optimizations. 3. Using a dedicated library for array manipulation, like Array.prototype.filter() and Array.prototype.reduce(). 4. Utilizing SIMD instructions (if supported by the browser) to accelerate the sorting step. Keep in mind that these alternatives might introduce additional complexity or trade-offs in terms of performance, readability, and maintainability. The benchmark results show that Chrome 104 on a Mac OS X 10.15.7 desktop executes the `sort + indexOf` approach faster (156489 executions per second) compared to the `.intersection()` method (93195 executions per second).
Related benchmarks:
uniqBy performance
lodash vs es6 in sort method
Sort lodash vs native
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?