Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.indexOf (numeric values)
(version: 0)
Comparing performance of:
includes vs lookup vs indexof
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ 8123075259360718, 2180349503224798, 6686356311324193, 362184468266422, 2306757200330954, 4532178674851796, 8970950942018765 ]; var b = new Set(a)
Tests:
includes
return a.includes(8123075259360718) ^ a.includes(7516472072139877) ^ a.includes(362184468266422);
lookup
return b.has(8123075259360718) ^ b.has(7516472072139877) ^ b.has(362184468266422);
indexof
return (a.indexOf(8123075259360718) > -1) ^ (a.indexOf(7516472072139877) > -1) ^ (a.indexOf(362184468266422) > -1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
indexof
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):
**Overview** The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test case compares three different approaches to check if a specific value exists in an array: using `array.includes()`, `Set` lookup (`b.has()`), and `array.indexOf()`. **Benchmark Definition** The benchmark definition is as follows: * Create an array `a` with six numeric values. * Create a new `Set` object `b` from the array `a`. Three individual test cases are defined, each using one of the three approaches to check if three specific values (`8123075259360718`, `7516472072139877`, and `362184468266422`) exist in the set `b` or array `a`. The test cases return a boolean value indicating whether all three values exist. **Options Compared** The options compared are: * **`array.includes()`**: This method checks if a specified value exists in an array. It returns `true` if the value is found, and `false` otherwise. * **`Set` lookup (`b.has()`)**: This method checks if a specified value exists in a `Set` object. It returns `true` if the value is present, and `false` otherwise. * **`array.indexOf()`**: This method returns the index of the first occurrence of a specified value in an array. If the value is not found, it returns `-1`. **Pros and Cons** Here are some pros and cons for each approach: * **`array.includes()`**: + Pros: Simple to use, widely supported. + Cons: May be slower than `Set` lookup or `indexOf()` for large arrays, as it scans the array from start to end. * **`Set` lookup (`b.has()`)**: + Pros: Generally faster than `array.includes()` and can be more efficient for large sets. + Cons: Requires creating a `Set` object, which may incur additional memory overhead. Not all browsers support `Set` objects. * **`array.indexOf()`**: + Pros: Can be faster than `array.includes()` for small arrays or when using cached results. + Cons: Returns the index of the first occurrence, not just a boolean result. May return `-1` if the value is not found. **Library and Special JS Features** The test case uses a `Set` object, which is a built-in JavaScript object that stores unique values. The `b.has()` method is used to perform lookup operations on the set. No special JavaScript features or syntax are required for this benchmark. **Other Alternatives** Alternative approaches could include: * Using `array.prototype.includes()` instead of `array.includes()`, which uses an iterator for better performance. * Using a more modern approach, such as using `Array.from()` and `includes()` in combination with `Set` lookup. * Using other data structures, such as a `Map` object, to store and retrieve values. However, these alternatives may not provide significant performance benefits or changes to the benchmark results.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
set vs array find if exists
set vs array find if exists v2
set.has vs. array.includes (find 999,999 in 1,000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?