Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs object key
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key]
Created:
5 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']
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
Object[key]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
201350080.0 Ops/sec
Object[key]
156360016.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** MeasureThat.net is testing the performance of two different approaches to check if an array contains a specific value: `array.includes()` and accessing an object using its key (`obj['sausage']`). **Script Preparation Code** The script preparation code generates a large array with 1000 random keys, where each key has a unique string value. The key is stored in the `obj` variable. ```javascript var obj = (new Array(1000)).fill(null).reduce((prev, newVal) => { prev[Math.random() + ''] = Math.random() + ''; return prev; }, { sausage: 'tst' }); ``` This code creates a large array and populates it with random keys using the `reduce()` method. The key is stored in an object (not explicitly shown in the provided code). **Html Preparation Code** The HTML preparation code is empty, which means that no additional setup or rendering is required for this benchmark. **Test Cases** There are two test cases: 1. **Includes** ```javascript "array.includes('sausage')" ``` This test case uses the `includes()` method to check if the array contains the string `'sausage'`. 2. **Object[key]** ```javascript "obj['sausage']" ``` This test case accesses the value associated with the key `'sausage'` in the `obj` object. **Library and Purpose** The `reduce()` method is a built-in JavaScript method used to apply a function to each element of an array and reduce it to a single output. In this benchmark, it's used to generate the random keys for the array. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. It only uses standard JavaScript methods and data structures. **Pros and Cons of Approaches** 1. **array.includes()** * Pros: + Simple and concise + Fast (it's a built-in method) * Cons: + May not be optimized for large arrays, potentially leading to slower performance 2. **Object[key]** * Pros: + Direct access to the value associated with a key * Cons: + May be slower due to the need to look up the key in the object **Other Alternatives** 1. `array.indexOf()` or `array.lastIndexOf()`: These methods can also be used to check if an array contains a specific value, but they may have different performance characteristics depending on the browser and JavaScript engine. 2. `Array.prototype.some()` or `Array.prototype.every()`: These methods can be used with callback functions to iterate over the array and check for the presence of a value. In summary, this benchmark is testing the performance of two simple yet distinct approaches to check if an array contains a specific value. The results will provide insight into how these different methods perform in various browsers and JavaScript engines.
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?