Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs map
(version: 0)
Comparing performance of:
indexOf vs map with forEach vs map with loop
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function hasWithIndexOf(needle, haystack) { return haystack.indexOf(needle) !== -1; } function hasWithMapForEach(needle, haystack) { var map = {}; haystack.forEach(item => map[item] = true); return map[needle]; } function hasWithMapLoop(needle, haystack) { var map = {}; for (var i=0, len=haystack.length; i<len; ++i) { map[haystack[i]] = true; } return map[needle]; } var testArray = []; for (var i=0; i<100; ++i) { testArray.push('00' + i); }
Tests:
indexOf
for (var i=0; i<100; ++i) { hasWithIndexOf('404', testArray); }
map with forEach
for (var i=0; i<100; ++i) { hasWithMapForEach('404', testArray); }
map with loop
for (var i=0; i<100; ++i) { hasWithMapLoop('404', testArray); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
map with forEach
map with loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
28422.4 Ops/sec
map with forEach
1565.8 Ops/sec
map with loop
1336.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Benchmark Definition** The benchmark definition is a set of three functions: 1. `hasWithIndexOf(needle, haystack)`: This function uses the `indexOf` method to search for the presence of `needle` in the `haystack` array. If found, it returns `true`. 2. `hasWithMapForEach(needle, haystack)`: This function creates a Map data structure and iterates over the `haystack` array using `forEach`. It sets the value of each item to `true`, effectively creating an array of booleans with the same length as the original array. Then, it checks if the `needle` is present in the resulting array by looking up its index. 3. `hasWithMapLoop(needle, haystack)`: This function creates a Map data structure and iterates over the `haystack` array using a traditional `for` loop. It sets the value of each item to `true`, just like in the previous example. Then, it checks if the `needle` is present in the resulting array by looking up its index. **Options Compared** The three functions are compared to measure their performance differences: * `indexOf`: Directly searching for the presence of a value using the `indexOf` method. * `map with forEach`: Using the `forEach` method to iterate over the array and create an array of booleans. * `map with loop`: Using a traditional `for` loop to iterate over the array and create an array of booleans. **Pros and Cons** Here's a brief summary of each approach: 1. **indexOf**: * Pros: Fast, straightforward, and widely supported. * Cons: May have performance issues for large arrays or if the value is not found. 2. **map with forEach**: * Pros: Easy to read, concise, and works well for modern browsers. * Cons: Can be slower due to the overhead of creating a new array and iterating over it. 3. **map with loop**: * Pros: Flexible, as it allows for manual control over the iteration process. * Cons: More verbose and may require more CPU cycles. **Library Usage** None of these functions rely on specific libraries or external dependencies, making them suitable for use in a variety of environments. **Special JavaScript Features** The benchmark uses the `forEach` method, which is a modern JavaScript feature introduced in ECMAScript 5. If you're using an older version of JavaScript that doesn't support `forEach`, you may need to use alternative iteration methods. **Alternatives** If you're looking for alternatives to these approaches, consider: 1. Using regular expressions instead of `indexOf` for searching. 2. Utilizing other data structures like `Set` or `Promise.all()` for iterating over arrays. 3. Leveraging browser-specific features like WebAssembly or native modules for performance optimization. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and target environment.
Related benchmarks:
indexOf vs map
indexOf vs map
indexOf vs map on smaller arrays
indexOf vs map iterator
Comments
Confirm delete:
Do you really want to delete benchmark?