Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs object key -1
(version: 0)
Comparing performance of:
array.includes vs obj[key]
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = (new Array(1000)).fill(null).reduce((prev, newVal) => {prev[Math.random() + ''] = Math.random() + ''; return prev; }, { sausage: 'tst' }); var array = Object.keys(obj);
Tests:
array.includes
array.includes('sausage')
obj[key]
!!obj['sausage']
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.includes
obj[key]
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/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.includes
25362622.0 Ops/sec
obj[key]
24070768.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents two JavaScript microbenchmarks: `array.includes` and `obj[key]`. The benchmarks are designed to measure the performance difference between using the `.includes()` method on an array and accessing object properties using bracket notation (`[]`) or double square bracket notation (`[][]`). **Options compared** Two approaches are being compared: 1. **Array.includes**: This approach uses the built-in `includes()` method on an array to search for a specific value. 2. **obj[key]**: This approach uses bracket notation to access an object property using a key. **Pros and Cons of each approach** * **Array.includes**: + Pros: Efficient, concise, and widely supported. + Cons: May be slower than direct access in some cases (e.g., when the array is sparse). * **obj[key]**: + Pros: Can be faster for sparse arrays or objects, as it only checks for existence, not value equality. + Cons: Less efficient and less concise than `includes()`, with more overhead due to the explicit key lookup. **Library used** The benchmark uses the built-in JavaScript methods `Object.keys()` and `reduce()` to create a large array of random objects. The `sausage` property is generated randomly for each object, but its value is not relevant to the benchmark. **Special JS feature or syntax** No special JavaScript features or syntax are mentioned in this benchmark. It only uses standard JavaScript language constructs and built-in methods. **Other alternatives** If you wanted to test alternative approaches, here are a few examples: * Using `indexOf()` instead of `includes()`: This method returns the index of the first occurrence of the specified value, or -1 if not found. * Using `in` operator: This operator checks if a property is present in an object. * Using `hasOwnProperty()` to check for existence in an object's own properties. Keep in mind that these alternatives might have different performance characteristics and may not be as efficient as the original approaches used in this benchmark.
Related benchmarks:
array includes (worst case) vs object key
Object.keys(object).includes(key) vs key in object
array includes vs object[key] vs key in object
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?