Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs set.has vs object property
(version: 0)
Comparing performance of:
array includes vs set has vs object property
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a); var c = {1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true}
Tests:
array includes
return a.includes(9)
set has
return b.has(9)
object property
return c[9]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array includes
set has
object property
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array includes
15838078.0 Ops/sec
set has
18365182.0 Ops/sec
object property
18242976.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three ways to check if an element exists in an array, set, or object: 1. `a.includes(9)` 2. `b.has(9)` 3. `c[9]` In each case, a test is run with the input `9`, which should be present in the collection. **Options Compared** The three options being compared are: * **Array includes**: Using the `includes` method to check if an element is present in an array. * **Set has**: Using the `has` method to check if an element is present in a set. * **Object property access**: Accessing a property directly on an object using square brackets (`[]`) to check if it exists. **Pros and Cons of Each Approach** 1. **Array includes**: * Pros: Simple, efficient, and widely supported. * Cons: May have performance issues for large arrays or when dealing with non-numeric keys. 2. **Set has**: * Pros: Fast lookups, efficient memory usage, and no array bounds checking. * Cons: Requires a set object to be created, which can be overkill if the array is small. 3. **Object property access**: * Pros: Simple, non-intrusive, and works with any type of object (not just arrays or sets). * Cons: May have performance issues for large objects or when dealing with complex key structures. **Library Used** None explicitly mentioned in the benchmark definition or test cases. However, it's likely that the `Set` class is used internally to create the set `b`. **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The code uses standard JavaScript constructs and doesn't rely on any experimental or advanced features. **Other Alternatives** If you want to explore alternative ways to check for element existence, consider: * Using a `for` loop to iterate over the array/set/object. * Using regular expressions (regex) to search for the element in the string. * Using a library like Lodash or Underscore.js for utility functions. Keep in mind that these alternatives might have different performance characteristics and may not be as efficient as the methods being compared in this benchmark.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
Small n set vs array
array.includes vs. set.has on the fly
Array includes vs Set.has
Comments
Confirm delete:
Do you really want to delete benchmark?