Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs map
(version: 0)
Comparing performance of:
indexOf vs map
Created:
8 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 = {}; array.forEach(item => map[item] = true); function hasWithMap(needle) { return needle in map; }
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:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
19521528.0 Ops/sec
map
577173.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data to understand what is being tested. **Benchmark Definition** The benchmark definition represents two different approaches to test performance: 1. `indexOf`: This approach uses the traditional `indexOf` method of arrays in JavaScript, which searches for an element from left to right and returns its index if found, or -1 if not. 2. `map`: This approach uses the `map()` function, which applies a specified function to each element of an array and returns a new array with the results. **Comparison** The comparison being made here is between these two approaches for searching for a specific value (`'404'`) in an array. **Pros and Cons:** * **indexOf**: + Pros: Simple, widely supported, and fast (in most cases). + Cons: May be slower for large arrays or when the element is not found at the beginning of the array. * **map**: + Pros: More flexible and can be used for other operations beyond just searching. Can also improve performance by avoiding unnecessary iterations. + Cons: Requires more memory as it creates a new array, which might be costly for large arrays. In general, `indexOf` is often preferred for simple searches, while `map` can provide benefits when the search is part of a larger operation or when you need to perform other operations on the array. **Library/Function** No libraries are explicitly mentioned in this benchmark definition. However, it's worth noting that both `indexOf` and `map()` are built-in functions in JavaScript, so no external dependencies are required. **Special JS feature/Syntax** The `forEach()` method is used in one of the benchmark definitions (`hasWithMap` function), which is a modern JavaScript feature introduced in ECMAScript 5. It allows iterating over an array and executing a provided callback function for each element without requiring explicit indexing or looping variables. For developers familiar with older versions of JavaScript, this might seem unfamiliar. However, `forEach()` has become a standard part of modern web development, and its usage is widely accepted. **Other Alternatives** Alternatives to these approaches could include: * Using other searching algorithms like binary search (less common in JavaScript). * Implementing a custom array implementation with built-in searching functions. * Using external libraries or frameworks that provide optimized searching capabilities. However, for general-purpose web development and most use cases, `indexOf` and `map()` remain popular choices due to their simplicity, performance, and widespread support.
Related benchmarks:
for vs map
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
for vs foreach vs map 2
Comments
Confirm delete:
Do you really want to delete benchmark?