Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript IndexOf vs Include vs map v2
(version: 0)
Comparing performance of:
IndexOf vs Includes vs Map vs Set
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [32, 37, 38, 39, 40]; var map = {}; var s = new Set(); for (const key of arr) { map[key] = true; s.add(key); }
Tests:
IndexOf
const found = Boolean(arr.indexOf(65))
Includes
const found = arr.includes(65)
Map
const found = Boolean(map[65])
Set
const found = s.has(65)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Map
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
11000478.0 Ops/sec
Includes
27830616.0 Ops/sec
Map
9867361.0 Ops/sec
Set
27989554.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on the provided JSON. **Benchmark Test Cases** The test cases are designed to compare the performance of three different approaches: 1. **IndexOf**: Using `arr.indexOf(65)` to search for the value 65 in an array. 2. **Includes**: Using `arr.includes(65)` to check if the array contains the value 65. 3. **Map**: Using `map[65]` to access a value in an object (the "map" variable). 4. **Set**: Using `s.has(65)` to check if a set contains the value 65. **Options Compared** These test cases compare the performance of: * Array-based search (`IndexOf`) * String-based search (`Includes`) * Object-based access (`Map`) * Set-based membership testing (`Set`) **Pros and Cons of Different Approaches** Here's a brief overview of the pros and cons of each approach: * **Array-Based Search (IndexOf)**: + Pros: Simple, efficient for large arrays. + Cons: May not be suitable for objects or sets with many keys. + Performance: O(n) in the worst case, but often faster than other methods due to caching. * **String-Based Search (Includes)**: + Pros: Similar performance to `IndexOf`, but more flexible for string-based searches. + Cons: May not be suitable for non-string values or large arrays. + Performance: O(n) in the worst case, similar to `IndexOf`. * **Object-Based Access (Map)**: + Pros: Fast access times for objects with many keys. + Cons: Requires an object with a key-value mapping. + Performance: O(1) on average, but can be slower for small arrays or sets. * **Set-Based Membership Testing (Set)**: + Pros: Fast membership testing and insertion/removal operations. + Cons: May not be suitable for large sets or objects with many values. + Performance: O(1) on average. **Library Used** The "map" variable in the test case uses an object as a mapping, which is a common use case for JavaScript's `Map` data structure. The `Set` data structure is used to store unique values. **Special JS Features or Syntax** None are mentioned in this specific benchmark. **Other Alternatives** Some alternative approaches that could be compared in this benchmark include: * Using regular expressions (e.g., `arr.match(/\d+/)`) for string-based searches. * Using array methods like `find()` or `forEach()` instead of indexing or includes. * Comparing performance with different data structures, such as linked lists or trees. However, these alternatives might not be as relevant to the specific use case and may introduce additional complexity.
Related benchmarks:
IndexOf vs includes js (idanlevi1)
Array from() vs Map.keys()
Array find with indexOf vs includes
array.includes vs array.indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?