Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Match 2 arrays, find vs fromEntries
(version: 4)
Comparing performance of:
find vs from Entries vs Reduce
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array1 = [] var array2 = [] var array = [] for (let i = 0; i < 1000; i++) { array1.push({ hash: `hash-1-${i}`, value: `value-1-${i}`, }) array2.push({ hash: `hash-2-${i}`, value: `value-2-${i}`, }) array.push({ key: `hash-1-${i}`, hash: `hash-1-${i}`, value: undefined, }) array.push({ key: `hash-2-${i}`, hash: `hash-2-${i}`, value: undefined, }) }
Tests:
find
for (let i = 0; i < array.length; i++) { const found = array1.find(({ hash }) => hash === array[i].hash) || array2.find(({ hash }) => hash === array[i].hash) array[i].value = found.value }
from Entries
const mapArray1 = Object.fromEntries( array1.map(({ hash, value }) => [hash, value]) ) const mapArray2 = Object.fromEntries( array2.map(({ hash, value }) => [hash, value]) ) for (let i = 0; i < array.length; i++) { const found = mapArray1[array[i].hash] || mapArray2[array[i].hash] array[i].value = found }
Reduce
const mapArray1 = array1.reduce((result, { hash, value }) => ({...result, hash: value}), {}) const mapArray2 = array2.reduce((result, { hash, value }) => ({...result, hash: value}), {}) for (let i = 0; i < array.length; i++) { const found = mapArray1[array[i].hash] || mapArray2[array[i].hash] array[i].value = found }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
find
from Entries
Reduce
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 its options. **Benchmark Overview** The benchmark measures the performance of three different approaches to find matching elements between two arrays: `Array.prototype.find()`, `Object.fromEntries()` with `map()`, and `Array.prototype.reduce()`. **Option 1: Array.prototype.find()** This approach uses the `find()` method to search for an element in one of the arrays that matches a given condition. In this case, it checks if the hash value exists in both `array1` and `array2`. Pros: * Simple and straightforward implementation. * Fast lookup times due to the optimized internal array storage. Cons: * Only works on arrays; not suitable for objects or other data structures. * Requires an exact match between the search key and the element being searched. **Option 2: Object.fromEntries() with map()** This approach converts both `array1` and `array2` into maps, then uses `map()` to transform each item in the arrays. It then looks up the value in the corresponding map using the hash key. Pros: * More flexible than `find()`, as it can handle objects or other data structures. * Can be more efficient for large datasets, since it avoids the overhead of iterating over arrays. Cons: * Requires creating intermediate maps and traversing them, which can add overhead. * May not perform well on very large datasets due to memory allocation and garbage collection. **Option 3: Array.prototype.reduce()** This approach uses the `reduce()` method to combine multiple iterations of a function into a single operation. In this case, it creates an accumulator object and updates its properties with the hash values from each array. Pros: * Can be more efficient than `map()` or `fromEntries()`, as it avoids unnecessary memory allocations. * Can handle large datasets efficiently due to its accumulator-based approach. Cons: * Requires understanding of the accumulator pattern and how it affects performance. * May have higher overhead for small datasets due to the function invocation. **Library Used** In this benchmark, no specific library is used. However, `Object.fromEntries()` uses the `Map` API under the hood. **Special JS Features/Syntax** None mentioned in the provided code or documentation. **Other Alternatives** For large-scale benchmarking and performance testing, consider using: * `BigInt`: for integer arithmetic and comparisons. * `Symbol`: for creating unique symbol keys. * `Array.prototype.every()`, `Array.prototype.some()`, `Object.keys()`, and other methods that can provide better performance than `find()` or `reduce()`. Keep in mind that the best approach depends on the specific use case, dataset size, and performance requirements.
Related benchmarks:
Spread vs Push to Same Array v1
Array#push vs Array#apply
Map vs preallocation vs push vs no fill
Object key access vs array find - #2
Map vs Preallocation (object)
Comments
Confirm delete:
Do you really want to delete benchmark?