Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs array.indexOf (integer values, small number of elements)
(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 = [ 1, 4, 3, 7, -4]; var b = new Set(a)
Tests:
includes
return a.includes(7)
lookup
return b.has(7)
indexof
return a.indexOf(7) >= 0
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):
Let's break down the provided benchmark and explain what's being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Overview** The provided benchmark measures the performance of three different ways to check if an element exists in an array or set: `array.includes()`, `set.has()` (using a Set data structure), and `array.indexOf()`. The benchmark uses integer values with a small number of elements, which is likely to be a microbenchmark. **Script Preparation Code** The script preparation code creates two variables: ```javascript var a = [ 1, 4, 3, 7, -4]; var b = new Set(a); ``` `a` is an array with five integer values, and `b` is a Set data structure created from the elements of `a`. **Benchmark Test Cases** There are three test cases: 1. **`includes`**: Returns `true` if `7` exists in `a`. 2. **`lookup`**: Uses the `has()` method on the `Set` object `b`, passing `7` as an argument. 3. **`indexof`**: Returns `>= 0` if the index of `7` in `a` is non-negative. **Library and Purpose** In this benchmark, the Set library (`Set`) is used to create a set from the elements of array `a`. The Set data structure provides fast lookups for its members. In this case, `b.has(7)` uses the Set's lookup method to check if `7` exists in the set. **Special JS Feature/Syntax** The benchmark does not explicitly use any special JavaScript features or syntax that would affect performance. It only relies on standard JavaScript language and built-in data structures. **Approach Comparison** Here are the approaches compared: 1. **`array.includes()`**: Uses a linear search algorithm to check if `7` exists in the array. 2. **`set.has()`**: Uses a hash table-based lookup mechanism, which is likely to be faster for small sets and integers. 3. **`array.indexOf()`**: Returns the index of the first occurrence of `7` in the array, or `-1` if it's not found. **Pros and Cons** * **`array.includes()`**: + Pros: Simple to implement, easy to understand. + Cons: Linear search algorithm can be slow for large arrays. * **`set.has()`**: + Pros: Fast lookup times due to hash table-based implementation. + Cons: Requires an extra data structure (Set) to create and manage. * **`array.indexOf()`**: + Pros: Can return the index of the first occurrence, which can be useful. + Cons: Returns `-1` if not found, making it less suitable for simple presence checks. **Other Considerations** When choosing between these approaches, consider the specific use case and requirements: * If you need to check if an element exists in a small array or set frequently, `set.has()` might be a good choice. * If you're working with large arrays and performance is critical, `array.includes()` might be a better option (although this benchmark suggests it's slower). * If you need to find the index of the first occurrence, `array.indexOf()` might be the best choice. **Alternative Approaches** Some alternative approaches to check if an element exists in an array or set include: * Using `includes()` with a callback function: `a.includes(7, 0)` * Using `indexOf()` with a specific start index * Creating a custom lookup function using bitwise operations or other optimization techniques However, these alternatives are not compared in this benchmark, and their performance may vary depending on the specific use case.
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
array.includes vs. set.has on the fly
Comments
Confirm delete:
Do you really want to delete benchmark?