Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs in operator
(version: 0)
Comparing performance of:
includes vs lookup vs in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for (let i = 0; i < 100_000; i++) { a.push(i); } var b = new Set(a) var c = {} for (const key of a) { c[key] = 0; }
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
in
return 9 in c
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
109843160.0 Ops/sec
lookup
623569728.0 Ops/sec
in
577336896.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition:** The provided JSON represents a benchmark test that compares the performance of three different ways to check if an element exists in an array: 1. `array.includes()` 2. `set.has()` 3. `in` operator Let's break down each option and their pros and cons: ### 1. `array.includes()`: * This method checks if a specified value (in this case, the number 9) exists in the array. * Pros: * Simple to implement * Widely supported by most JavaScript engines * Fast performance * Cons: * May be slower for very large arrays due to the need to iterate through each element * Can be less efficient than other methods if the array is already sorted or has some other property that can be used to speed up the lookup ### 2. `set.has()`: * This method checks if a specified value exists in a Set object. * Pros: * Efficient for large datasets since Sets only store unique values * Fast performance due to the use of hash tables internally * Can be faster than array.includes() for very large arrays * Cons: * Requires creating an additional Set object, which can incur overhead in terms of memory allocation and garbage collection * May not be suitable for small or empty datasets ### 3. `in` operator: * This method checks if a specified value exists in the array using the `in` keyword. * Pros: * Simple to implement * Fast performance since it only needs to access the array's prototype chain * Can be faster than array.includes() for small arrays or when working with objects that have inherited properties from the array's prototype * Cons: * May not work correctly in all cases (e.g., when using a custom array implementation) * Can be slower than `set.has()` for very large arrays due to the need to access each element **Library/ Framework:** The benchmark script uses JavaScript's built-in data structures and operators. There are no external libraries or frameworks involved. **Special JS feature/Syntax:** There is no special JavaScript feature or syntax used in this benchmark. It only relies on standard JavaScript language features. **Other Alternatives:** If you're interested in exploring alternative ways to check if an element exists in an array, here are a few options: * **Binary Search:** For sorted arrays, binary search can be an efficient way to find an element. * **Hash Table Lookups:** If the array is already indexed or has a unique identifier for each element, using a hash table lookup can be faster than linear searches. * **Fibonacci Search:** Another alternative method for finding an element in a sorted array is the Fibonacci search algorithm. Keep in mind that these alternatives may have different performance characteristics depending on the specific use case and data distribution.
Related benchmarks:
Array construct vs array push
set.has vs. array.includes large 1
set.has vs. array.includes (1 million entries)
set.has vs. array.includes - large array
Comments
Confirm delete:
Do you really want to delete benchmark?