Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.hasOwn vs Object in vs Object[] vs Array.indexOf vs Array.includes
(version: 0)
Comparing performance of:
Object.hasOwnProperty vs Object in vs Array.indexOf vs direct vs Array includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var object = {}, array = [], i, test = 1000; for (i = 0; i < 20; i++) { object['something' + i] = true; array.push('something' + i); }
Tests:
Object.hasOwnProperty
object.hasOwnProperty('something' + test)
Object in
('something' + test) in object
Array.indexOf
array.indexOf('something' + test) !== -1
direct
object['something' + test] === true
Array includes
Object.keys(object).includes('something' + test)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Object.hasOwnProperty
Object in
Array.indexOf
direct
Array includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of four different ways to check if a property exists in an object: 1. `Object.hasOwnProperty` 2. `(key) in object` (using the "in" operator) 3. `array.indexOf(key) !== -1` (checking if the key is not found at the end of the array) 4. `object[key] === value` (direct access to the property) **Options Compared** The benchmark compares these four options because they are all commonly used in JavaScript to check if a property exists, but they may have different performance characteristics. **Pros and Cons of Each Approach** 1. **`Object.hasOwnProperty`**: This approach is often considered the most efficient and straightforward way to check if a property exists on an object. It's also widely supported by browsers. * Pros: Fast, widely supported, easy to read. * Cons: May not be as readable for some developers who are used to other approaches. 2. **`(key) in object`**: This approach uses the "in" operator to check if a key exists in an object. It's often considered more concise and expressive than `Object.hasOwnProperty`. * Pros: Concise, expressive, works well with objects that have a lot of properties. * Cons: May be slower than `Object.hasOwnProperty` due to the overhead of the "in" operator. 3. **`array.indexOf(key) !== -1`**: This approach checks if the key is not found at the end of the array using `indexOf`. It's often used when working with arrays instead of objects. * Pros: Fast, widely supported, works well with arrays. * Cons: May be slower than `Object.hasOwnProperty` or `(key) in object`, as it requires searching the entire array. 4. **`object[key] === value`**: This approach directly accesses the property using bracket notation. It's often considered a simple and straightforward way to check if a property exists. * Pros: Fast, simple, widely supported. * Cons: May be slower than `Object.hasOwnProperty` or `(key) in object`, as it requires accessing the property directly. **Library Used** There is no explicit library used in this benchmark. However, the "in" operator and bracket notation are built-in JavaScript features that are widely supported by browsers. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax beyond what's built into the language.
Related benchmarks:
Object.hasOwnProperty vs Object in vs Object[] vs Array.indexOf vs Array.includes vs Reflect.has()
Object.hasOwnProperty vs Object in vs Object[] vs Array.indexOf vs Array.includes1231231
Object.hasOwnProperty vs Object in vs Object[] vs Array.indexOf vs Array.includes times 100000
Object.hasOwnProperty vs Object in vs Object[] vs Array.indexOf vs Array.includes times 100000000
Comments
Confirm delete:
Do you really want to delete benchmark?