Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array includes vs Object key lookup vs Object hasOwnProperty
(version: 0)
Compares performance of Array.prototype.includes() vs Object key lookup vs Object.prototype.hasOwnProperty()
Comparing performance of:
Array includes vs Object key lookup vs Object hasOwnProperty
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; var b = Object.freeze({ 'one': true, 'two': true, 'three': true, 'four': true, 'five': true, 'six': true, 'seven': true, 'eight': true, 'nine': true, 'ten': true });
Tests:
Array includes
return a.includes('eight')
Object key lookup
return b['eight'] === true
Object hasOwnProperty
return b.hasOwnProperty('eight')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array includes
Object key lookup
Object hasOwnProperty
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 what's being tested in the provided JSON. **Benchmark Definition** The benchmark compares the performance of three different approaches: 1. **Array.prototype.includes()**: This method checks if a specified value exists in an array. In this case, it searches for the string "eight" in the array `a`. 2. **Object key lookup**: This approach uses the bracket notation (`b['eight'] === true`) to access a property in an object `b`. The property is set to the value `true`, and we're checking if its existence matches what `Array.prototype.includes()` would return. 3. **Object.prototype.hasOwnProperty()**: Similar to object key lookup, this method checks if an object has a specific property with the given name (`b.hasOwnProperty('eight')`). However, it's more efficient because it doesn't need to check all properties of the object. **Options Comparison** The pros and cons of each approach are: * **Array.prototype.includes()**: Pros: widely supported, easy to use. Cons: can be slow for very large arrays or when searching for a specific value in a large array. * **Object key lookup**: Pros: efficient for objects with many properties, as it only checks the requested property. Cons: requires knowledge of bracket notation and object property access. * **Object.prototype.hasOwnProperty()**: Pros: faster than object key lookup, especially for smaller objects or when searching for an exact match. Cons: not all browsers support this method, and its usage can be more verbose. **Library Usage** In the provided code, `Object.freeze()` is used to make the object `b` immutable. This is a utility function in JavaScript that converts an object into an immutable object. **Special JS Feature or Syntax** There are no special features or syntaxes being tested here, as all methods are standard ECMAScript methods. **Other Considerations** * The benchmark uses Chrome 115, Windows 10, and a desktop device. This might limit the results to this specific browser and platform. * The benchmark is run on the same machine, which could lead to inconsistent results if the environment changes (e.g., upgrading the OS or hardware). * The benchmark does not consider other factors that might affect performance, such as array size, object complexity, or concurrent execution. **Alternative Benchmarks** If you wanted to explore alternative approaches, you might consider: * Using `Array.prototype.indexOf()` instead of `includes()`. * Implementing a custom search function for arrays using binary search. * Comparing the performance of different object implementations (e.g., `Object.create()`, `Object.fromEntries()`) or data structures (e.g., `Set` vs. `Map`).
Related benchmarks:
undefined vs. hasOwnProperty
undefined vs. hasOwnProperty2
Object.prototype.hasOwnProperty vs obj.hasOwnProperty
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?