Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search object property value in array of object - 2
(version: 1)
Comparing performance of:
map indexof vs custom func vs findIndex
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var size = 10000 var arr = [] for (let i = 0; i < size; i++) { arr.push({ i }) } var research = 6578;
Tests:
map indexof
const index = arr.map(e => e.i).indexOf(research)
custom func
function findObjectInArray(arr, prop, value) { if (arr.length === 0 || !arr[0].hasOwnProperty(prop)) return -1; for (let i = 0; i < size; i++) { if (!arr[i].hasOwnProperty(prop)) continue; if (arr[i][prop] === value) return i; } return -1; } const index = findObjectInArray(arr, 'i', research)
findIndex
const index = arr.findIndex(e => e.i === research)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map indexof
custom func
findIndex
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):
Measuring JavaScript performance is crucial, and tools like MeasureThat.net provide valuable insights. **What's being tested?** The provided benchmark tests the performance of three different approaches to search for an object property value in an array of objects: 1. `map()` with `indexOf()`: This approach uses the `map()` method to create a new array with the property values, and then searches for the target value using `indexOf()`. 2. A custom function (`findObjectInArray()`) that manually iterates through the array to find the target value. 3. `findIndex()`: This approach uses the `findIndex()` method, which returns the index of the first element that satisfies the provided condition. **Options comparison** Here's a brief overview of each option: * `map()` with `indexOf()`: + Pros: Simple and widely supported. The `map()` method is efficient for creating new arrays, and `indexOf()` is a familiar search function. + Cons: May incur additional overhead due to the creation of an intermediate array. The search operation itself can be slower than other approaches. * Custom function (`findObjectInArray()`): + Pros: Fine-grained control over the search process allows for optimization, and it doesn't require the creation of an intermediate array. + Cons: More complex and harder to maintain than the `map()` with `indexOf()` approach. The function needs to handle edge cases and potential performance bottlenecks. * `findIndex()`: + Pros: Optimized for searching in arrays, as it only scans through elements until finding a match or reaches the end of the array. + Cons: Less widely supported than `map()` with `indexOf()`. The method is also more expensive due to its optimized search algorithm. **Library usage** The `findIndex()` method uses the built-in JavaScript Array.prototype.findIndex() method, which is available in most modern browsers. This approach leverages a specialized optimization for finding an element that satisfies a given condition. **Special JS features** There are no specific JavaScript features or syntax mentioned in the benchmark definitions. However, some notes on these approaches: * The use of `map()` with `indexOf()` relies on the fact that `indexOf()` is a string-based method and might not work correctly for non-string property values. * The custom function (`findObjectInArray()`) demonstrates manual iteration over an array, which can be more intuitive but also more error-prone. **Other alternatives** To further improve performance or explore alternative approaches, you could consider the following: * Using `filter()` with a callback function instead of `indexOf()`. * Leveraging typed arrays (e.g., Int32Array) for faster searches. * Utilizing WebAssembly or native modules for optimized computations. * Employing parallel processing techniques to take advantage of multiple CPU cores. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case, performance requirements, and the desired level of complexity.
Related benchmarks:
Preinitialized array size vs Push operations to an empty one.
array last element big data
Array spread vs push 2
Test array and unshift
+ vs Number, with 100k numbers
Comments
Confirm delete:
Do you really want to delete benchmark?