Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs object[key] vs key in object
(version: 0)
Performance test on array.includes, object[key] accessor and key in object check
Comparing performance of:
Includes vs Object[key] vs key in object
Created:
3 years ago
by:
Guest
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:
Includes
array.includes('sausage')
Object[key]
obj['sausage']
key in object
'sausage' in obj
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
Object[key]
key in object
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
24889806.0 Ops/sec
Object[key]
23664464.0 Ops/sec
key in object
23021070.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. The provided JSON represents a benchmark test that compares the performance of three different approaches: 1. **Array includes**: Using the `includes` method to check if an element exists in an array. 2. **Object[key]**: Accessing a property using the square bracket notation (`obj['sausage']`) and comparing it to the value `'sausage'`. 3. **key in object**: Using the `in` operator to check if a key exists in an object. **Options compared:** * Array includes * Object[key] (square bracket notation) * Key in object (`in` operator) **Pros and Cons of each approach:** 1. **Array includes**: * Pros: Easy to read and write, widely supported by browsers. * Cons: Can be slower than other approaches for large arrays or when dealing with sparse arrays. 2. **Object[key]** (square bracket notation): * Pros: Fast and efficient, especially when working with objects that have a small number of properties. * Cons: Requires knowledge of the property name and can lead to errors if not used correctly. 3. **Key in object** (`in` operator): * Pros: Quick and easy to use, suitable for most cases where you need to check if a key exists in an object. * Cons: May be slower than array includes or object[key] for very large objects. **Library usage:** In the provided JSON, the `Object.keys()` method is used to create an array of property names from the `obj` object. This is not a library per se but rather a built-in JavaScript method that returns an array of strings representing the property names in the specified object. However, it's worth noting that this approach assumes that all properties are enumerable and do not have any special characters. **Special JS feature or syntax:** The `reduce()` method used to populate the `obj` array with random values is a common JavaScript technique for creating arrays. The syntax `var array = Object.keys(obj);` uses the spread operator (`...`) and destructuring assignment (`Object.keys(obj)`) features of modern JavaScript. **Other alternatives:** If you need more control over the iteration process or want to optimize performance, you can consider using: * **Array.prototype.forEach()**: Iterate over an array using a callback function. * **Array.prototype.map()**: Create a new array by applying a transformation function to each element in the original array. * **For loops**: Use traditional for loops to iterate over arrays or objects. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the methods tested 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 -1
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?