Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
diff options
(version: 1)
Comparing performance of:
set-has vs indexOf vs smart indexOf
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
n = 80000 existing = Array.from({length: n}, () => ({externalId: Math.floor(Math.random() * n)})); desired = Array.from({length: n}, () => ({externalId: Math.floor(Math.random() * n)})); key = 'externalId'
Tests:
set-has
desiredKeys = new Set(desired.map(d => d[key])) existingKeys = new Set(existing.map(e => e[key])) return desired.filter(d => !existingKeys.has(d[key]))
indexOf
existingKeys = existing.map(e => e[key]).sort() return desired.filter(d => existingKeys.indexOf(d[key]) === -1)
smart indexOf
desiredSort = desired.sort((a,b) => a[key] - b[key]) existingKeys = existing.map(e => e[key]).sort() return desiredSort.reduce((acc, d) => { if (i = existingKeys.indexOf(d[key]) > -1) { acc.i = i; acc.list.push(d); } return acc; }, {i: 0, list: []})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
set-has
indexOf
smart 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 break down what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares three different approaches to find the external IDs that are present in one array but not in another. The arrays contain random data with 80,000 elements each. The goal is to measure which approach is the fastest. **Approaches Compared** 1. **set-has**: This approach uses a `Set` data structure to efficiently check for membership. It creates a set of desired external IDs and then iterates through the existing array to find any IDs that are not present in the set. 2. **indexOf**: This approach uses the `indexOf` method of the Array prototype to search for an element in the array. It first sorts both arrays, and then searches for each desired ID in the sorted existing array. 3. **smart indexOf**: This approach is similar to the previous one, but instead of sorting the entire existing array, it only keeps track of the index of the current element being processed. This reduces the time complexity from O(n log n) to O(n). **Pros and Cons** * **set-has**: Pros - efficient for large datasets, easy to implement. Cons - may not be suitable for small arrays or datasets where the overhead of creating a set is significant. * **indexOf**: Pros - simple to understand and implement. Cons - has a higher time complexity due to sorting and indexing operations. * **smart indexOf**: Pros - optimized for performance in large datasets, easy to understand. Cons - requires more memory due to keeping track of indices. **Library/Function Used** The `Set` data structure is used in the `set-has` approach. A set is a collection of unique values that allows for efficient membership testing. **Special JS Feature/Syntax** None are mentioned in this benchmark, but it's worth noting that the use of `Array.from()` to create new arrays and the `map()`, `filter()`, and `reduce()` methods are all modern JavaScript features used here. **Other Alternatives** Some alternative approaches could be: * Using a `Map` data structure instead of a set for membership testing. * Using a hash table-based approach, where each external ID is mapped to an index or a flag indicating its presence. * Implementing a custom data structure, such as a skip list or a balanced search tree, to optimize the search process. However, these alternatives may not provide significant performance improvements over the approaches tested in this benchmark.
Related benchmarks:
Fill array with random integers
Unique Array: Lodash vs spread new Set vs reduce vs for - random data
array vs float64 for io and slice
Array.from VS spreading for
Array.from() VS Spread
Comments
Confirm delete:
Do you really want to delete benchmark?