Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs Map object
(version: 0)
Comparing performance of:
indexOf vs map
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (var i=0; i<300; ++i) { array.push('00' + i); } function hasWithIndexOf(needle) { return array.indexOf(needle) !== -1; } var map = new Map(); array.forEach(item => map.set(item, true)); function hasWithMap(needle) { return map.get(needle); }
Tests:
indexOf
for (var i=0; i<100; ++i) { hasWithIndexOf('404'); }
map
for (var i=0; i<100; ++i) { hasWithMap('404'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 YaBrowser/24.7.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
29249.9 Ops/sec
map
124795.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark definition represents two test cases: `indexOf` and `map`. Both tests aim to measure the performance of finding an element in a list using two different approaches: 1. **Array methods (`indexOf`)**: The first test uses the `array.indexOf()` method to search for the presence of a specific value ("404") within the generated array. 2. **Map object**: The second test utilizes a Map object, where each element in the original array is used as a key and set with a corresponding true value. **Options Compared** The two tests compare the performance of: 1. **Array-based search (`indexOf`)**: This approach uses the `array.indexOf()` method to find an element. 2. **Map object search (`map`)**: This approach utilizes a Map object, where each key-value pair is set up in advance. **Pros and Cons** ### Array-based Search (`indexOf`) Pros: * Wide browser support * Easy to implement * Fast for small arrays Cons: * Can be slow for large arrays due to iteration * May have performance issues with edge cases (e.g., `undefined` or `null` values) ### Map Object Search (`map`) Pros: * Faster for large arrays, as it uses hash-based lookups * More efficient than array-based search for certain use cases Cons: * Requires creating a map object upfront * May have browser support issues (although supported by most modern browsers) * Less intuitive for beginners **Other Considerations** 1. **Array Iteration**: The `indexOf` method in JavaScript performs an iterative search through the array, making it less efficient than hash-based lookups like Map objects. 2. **Cache Efficiency**: Modern web browsers use various caching mechanisms to improve performance. This benchmark may not reflect real-world cache behavior. **Library and Special JS Features** No notable libraries are used in this benchmark. However, JavaScript's built-in `Map` object is employed, which provides a more efficient data structure for searching large datasets. **No Special JS Features are Used** No special JavaScript features or syntax are used beyond the standard array methods and Map objects. **Alternatives** Other alternatives to consider when implementing similar benchmarks: 1. **Binary Search**: For arrays with unique values, binary search can provide better performance than linear iteration. 2. **Set Data Structures**: Sets can offer faster lookups than maps for certain use cases, especially when dealing with large datasets. 3. **Other JavaScript Libraries or Frameworks**: Depending on the specific requirements and constraints of your project, you might consider using libraries like Lodash or Underscore.js to provide additional functionality. Keep in mind that this benchmark focuses specifically on comparing array-based search (`indexOf`) with Map object search (`map`). The alternatives listed above are not necessarily relevant to this particular test case.
Related benchmarks:
indexOf(needle) vs includes(needle) vs (needle in map) vs map.has(needle) (over 10,000 records)
indexOf vs map vs map2
indexOf vs map vs Set vs native Map
indexOf vs map on smaller arrays
indexOf vs map iterator
Comments
Confirm delete:
Do you really want to delete benchmark?