Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf(needle) vs includes(needle) vs (needle in map) vs map.has(needle) (over 10,000 records)
(version: 1)
Comparing performance of:
array.indexOf vs map.has vs array.includes vs in obj
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [] for (var i=0; i<10000; ++i) { array.push('00' + i) } function hasWithIndexOf(needle) { return array.indexOf(needle) !== -1 } function hasWithIncludes(needle) { return array.includes(needle) } var map = new Map array.forEach(item => map[item] = true) var obj = {} array.forEach(item => obj[item] = true) function hasWithMap(needle) { return map.has(needle) } function hasWithIn(needle) { return needle in map }
Tests:
array.indexOf
for (var i=0; i<100; ++i) { hasWithIndexOf('404'); }
map.has
for (var i=0; i<100; ++i) { hasWithMap('404'); }
array.includes
for (var i=0; i<100; ++i) { hasWithIncludes('404'); }
in obj
for (var i=0; i<100; ++i) { hasWithIn('404'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array.indexOf
map.has
array.includes
in obj
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 benchmark and its test cases. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares four different approaches to check if an element exists in an array: 1. `indexOf(needle)` 2. `includes(needle)` 3. `(needle in map)` 4. `map.has(needle)` These methods are used to check if a specific value (`needle`) is present in the array. **Options Compared** The benchmark compares two arrays with 10,000 records each, and four different approaches to check for the existence of a particular value in these arrays: 1. **`indexOf(needle)`**: This method returns the index of the first occurrence of `needle` in the array. If `needle` is not found, it returns -1. 2. **`includes(needle)`**: This method returns a boolean indicating whether `needle` is present in the array. 3. **`(needle in map)`**: This approach uses the `in` operator to check if `needle` is present as a key in the `map`. 4. **`map.has(needle)`**: This method checks if `needle` is present as a key in the `map`. **Pros and Cons** Here's a brief summary of each approach: 1. **`indexOf(needle)`**: * Pros: Fast, efficient, and widely supported. * Cons: Returns an index value, which may not be suitable for all use cases (e.g., when the array is very large). 2. **`includes(needle)`**: * Pros: More concise and readable than `indexOf`. * Cons: May be slower than `indexOf` due to the extra operation. 3. **`(needle in map)`**: * Pros: Lightweight, easy to implement, and works well for small maps. * Cons: Can be slower than `map.has` due to the extra lookup. 4. **`map.has(needle)`**: * Pros: Fast, efficient, and specifically designed for checking presence in a map. * Cons: May not work as expected if the map is very large (due to caching issues). **Library** The benchmark uses the following libraries: 1. `Map`: A built-in JavaScript data structure used to store key-value pairs. 2. No external libraries are mentioned in the provided JSON. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax, such as async/await, Promises, or ES6+ syntax. **Alternative Approaches** Other approaches to check if an element exists in an array include: 1. Using a `Set` data structure (e.g., `new Set(array).has(needle)`). 2. Implementing a custom iterative approach using loops and conditional statements. 3. Using a library like Lodash, which provides a `includes()` function. Keep in mind that the benchmark's results may vary depending on the specific JavaScript engine, browser, or version used to run the tests.
Related benchmarks:
indexOf vs map vs map2
indexOf vs map vs Set vs native Map
indexOf vs map iterator
Array.find vs Array.indexOf vs Array.filter vs Map.has
Comments
Confirm delete:
Do you really want to delete benchmark?