Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array include vs object key vs Set has V2
(version: 1)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Array.includes() vs Object[key] vs Set.has()
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var reports = (new Array(200)).fill(null).reduce((prev, newVal) => {prev.push({type: Math.random() + ''}); return prev; }, [{ type: 'core' }]); var SERVICES = [{ type: 'core' }, { type: 'foo' },{ type: 'bar' }];
Tests:
Array.includes()
var has; var array = SERVICES.map((service) => service.type); for (report of reports) { if(array.includes(report.type)) { has = true; } }
Object[key]
var has; var object = SERVICES.reduce((result, service) => { result[service.type] = service.type; return result; }, {}); for (report of reports) { if(object[report.type]) { has = true; } }
Set.has()
var has; var set = new Set(SERVICES.map((service) => service.type)); for (report of reports) { if(set.has(report.type)) { has = true; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes()
Object[key]
Set.has()
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; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes()
152235.7 Ops/sec
Object[key]
177884.5 Ops/sec
Set.has()
185267.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance and comparing approaches is crucial in software development, especially when it comes to optimizations and micro-optimizations. The provided benchmark measures the performance of three different methods for checking if an array contains a specific value: 1. **Array includes()**: This method uses the `includes()` function on the array. 2. **Object[key]**: This method uses bracket notation (`object[key]`) to access the property with the given key. 3. **Set.has()**: This method uses the `has()` function on a Set object. **Options Compared:** * `Array.includes()`: This option checks if an array contains a specific value using the `includes()` function, which iterates over the array elements until it finds a match or reaches the end of the array. * `Object[key]`: This option uses bracket notation to access properties on an object. It's typically faster than `Array.includes()`, as it avoids iterating over all array elements and instead only accesses the property directly. * `Set.has()`: This option adds an element to a Set object, which automatically performs a lookup operation when you call `has()` on it. The performance of this method depends on the size of the Set. **Pros and Cons:** * **Array includes():** * Pros: * Widely supported across browsers. * Easy to understand and implement. * Cons: * Iterates over all array elements, making it less efficient for large arrays. * **Object[key]:** * Pros: * Fast because it directly accesses the property without iterating over all array elements. * Can be a good option when working with objects instead of arrays. * Cons: * Only works if the key exists on the object; otherwise, will return `undefined`. * **Set.has():** * Pros: * Fast because it uses a lookup operation on the Set object, which has an average time complexity of O(1). * Cons: * Requires creating a Set object, which can be slower than simple array operations. * May not work well for very large arrays due to memory constraints. **Library and Purpose:** * None of the three methods rely on external libraries. They are built-in functions or standard methods in JavaScript. **Special JS Features or Syntax:** None mentioned, but keep in mind that `Set.has()` only works when a Set object is created, which requires initializing it with elements. This can be an extra step compared to other methods. **Alternatives:** * **Iterating over the array using a for loop**: Instead of using `Array.includes()`, you could manually iterate over the array using a `for` loop and check if each element matches the target value. * **Using `Map` or `Reduce()`**: For large arrays, using `Map` or `Reduce()` can be more efficient than simple iteration. However, this approach requires additional setup and understanding of Map/Reduce functions. Overall, these three methods offer different trade-offs between performance, simplicity, and use cases. The choice of method depends on the specific requirements of your project and the constraints you're working with.
Related benchmarks:
array includes vs object key
array includes (worst case) vs object key
array includes vs object key (small)
array includes vs object key vs set
Comments
Confirm delete:
Do you really want to delete benchmark?