Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search Object in Array/Map/Set - JavaScript performance
(version: 0)
Comparing performance of:
Arr - findIndex vs Arr - indexOf vs Arr - filter vs Arr - find vs Map - get vs Set - has vs Set - findInSet
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = new Array(15000); data = data.fill({ id: 0 }).map((el, idx) => el.id = idx); var mapData = new Map(data.map(obj => [obj.id, obj])) var setData = new Set(data) var recId = Math.floor(Math.random() * 15000); var findInSet = (obj, testFn) => { for (var item of obj) if(testFn(item)) return item; } var matchFn = (obj) => obj.id === recId
Tests:
Arr - findIndex
var index = data.findIndex((num) => num === recId);
Arr - indexOf
var index = data.indexOf(recId);
Arr - filter
var index = data.filter((obj) => obj.id === recId);
Arr - find
var index = data.find((obj) => obj.id === recId);
Map - get
var index = mapData.get(recId);
Set - has
var index = setData.has(data[recId]);
Set - findInSet
var index = findInSet(setData, matchFn);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
Arr - findIndex
Arr - indexOf
Arr - filter
Arr - find
Map - get
Set - has
Set - findInSet
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 world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition and Script Preparation Code** The benchmark definition represents a JavaScript function that performs a search operation on an array, map, or set data structure. The script preparation code generates a large array, map, and set with 15,000 elements each, filled with random data. A random index `recId` is generated, which will be used as the target value for the search operations. The script preparation code also defines two functions: `findInSet`, which performs a custom search operation on the set data structure, and `matchFn`, which takes an object as input and returns `true` if its `id` property matches the `recId`. **Individual Test Cases** There are six test cases, each representing a different search operation: 1. **Arr - findIndex**: Finds the index of the target value in the array using `Array.prototype.findIndex`. 2. **Arr - indexOf**: Finds the index of the target value in the array using `Array.prototype.indexOf`. 3. **Arr - filter**: Filters the array to get an array with only the elements that match the target value. 4. **Arr - find**: Finds the first element in the array that matches the target value using `Array.prototype.find`. 5. **Map - get**: Retrieves the value associated with the target key from the map using `Map.prototype.get`. 6. **Set - has**: Checks if the set contains an element that matches the target value using `Set.prototype.has`. 7. **Set - findInSet**: Performs a custom search operation on the set data structure using the `findInSet` function. **Comparison of Options** Here's a brief overview of each option and their pros and cons: * **Arr - findIndex**: This is generally considered the fastest way to find an index in an array. However, it may not work correctly if the target value is not present in the array. * **Arr - indexOf**: Similar to `findIndex`, but returns `-1` if the target value is not found. It's slightly slower than `findIndex`. * **Arr - filter**: This option creates a new array with only the matching elements, which can be slower and more memory-intensive than finding an index or using `find`. * **Arr - find**: Finds the first matching element in the array, but it may not work correctly if there are multiple matching elements. * **Map - get**: Retrieves a value from the map by its key. This is generally faster than searching through the keys of the map. * **Set - has**: Checks if an element is present in the set. This is generally fast, but can be slower for very large sets. * **Set - findInSet**: The custom search function defined in the script preparation code. Its performance will depend on the implementation and the size of the set. **Other Considerations** When interpreting benchmark results, keep in mind: * The device platform and operating system used to run the benchmark can affect performance. * The number of executions per second (ExecutionsPerSecond) is a good indicator of performance, but it's not the only factor. Lower values may indicate slower code or memory issues. * The benchmark results are not necessarily representative of real-world usage scenarios. Overall, this benchmark compares different search operations on arrays, maps, and sets in JavaScript. Understanding the pros and cons of each option can help you choose the best approach for your specific use case.
Related benchmarks:
Search: Array to Map and find vs Array.find
array.find vs object by key
Map.get versus Array.find for 100 elements
Search: Array to Map and find vs Array.find685000
Search: Array to Map and find vs Array.find 2
Comments
Confirm delete:
Do you really want to delete benchmark?