Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.IndexOf vs new Set.has
(version: 2)
array.indexOf vs new Set.has
Comparing performance of:
withIndexOf vs withSetHas
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const arr = []; for (let i = 0; i < 1000000; ++i) { arr.push(i); } function withIndexOf(num) { return arr.indexOf(num) !== -1; } function withSetHas(num) { const set = new Set(arr); return set.has(num); }
Tests:
withIndexOf
withIndexOf(999999);
withSetHas
withSetHas(999999);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
withIndexOf
withSetHas
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
withIndexOf
10121.5 Ops/sec
withSetHas
18.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark compares two approaches to find an element in an array: `array.indexOf` and `new Set.has`. The script prepares an array of 1 million elements, then defines two functions: * `withIndexOf(num)`: Returns `true` if `num` is found in the array using `array.indexOf`. * `withSetHas(num)`: Creates a set from the array, then checks if `num` is in that set using `new Set.has`. **Options Compared** The benchmark compares two options: 1. **array.indexOf**: This method searches for an element in the array by iterating through it and checking each element's value. 2. **new Set.has**: This method uses a data structure called a set to store unique values. It checks if a given value is in the set. **Pros and Cons** * `array.indexOf`: + Pros: Simple, easy to understand, and widely supported. + Cons: Can be slow for large arrays (O(n) time complexity), and it can return -1 if the element is not found, which may require additional checks. * `new Set.has`: + Pros: Fast (O(1) time complexity on average), efficient for large datasets, and returns a boolean value directly indicating presence or absence. + Cons: Requires creating an additional data structure (the set), and it's only available in modern browsers that support sets. **Library and Syntax** The benchmark uses the `Set` object, which is a built-in JavaScript API. The `new Set()` constructor creates a new set from an array, and the `has()` method checks for presence in the set. This syntax is widely supported in modern browsers. No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** For large datasets, you might also consider using other data structures like: * Tuples (similar to sets) for faster lookups * Binary search trees (BSTs) for balanced searching * Hash tables (e.g., `Map` in JavaScript) for fast key-value lookups However, these alternatives may have additional complexity and overhead compared to the simple set-based approach used in this benchmark. When running benchmarks like this one, it's essential to consider factors like memory allocation, garbage collection, and caching effects on performance. This test case provides a good starting point for evaluating the relative performance of `array.indexOf` vs `new Set.has`.
Related benchmarks:
IndexOf vs Includes array of numbers
indexOf vs findIndex with a simple case
findIndex vs indexOf for simple array 2
Array find with indexOf vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?