Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs object key (small)
(version: 0)
Performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key]
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
Object[key]
Script Preparation code:
var obj = (new Array(10)).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:
22 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
132031288.0 Ops/sec
Object[key]
190685904.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The test compares two ways to check if an array contains a value: 1. **`array.includes('sausage')`**: checks if a specific string is present in the array using the `includes()` method. 2. **`obj['sausage']`**: accesses a property of an object (`obj`) with the key `'sausage'`. **Options Compared** The benchmark compares two approaches: 1. **`array.includes('sausage')`** * Pros: + Simple and concise syntax. + Works well for most use cases. + Cons: + Can be slower than other methods, especially for large arrays or complex searches. + May not work as expected if the array contains non-string values. 2. **`obj['sausage']`** * Pros: + Fast and efficient for accessing specific properties of objects. + Works well for most use cases involving property lookups. Cons: + Requires knowledge of object notation (`'sausage'`) to access the desired property. + May not work as expected if the object is large or complex. **Other Considerations** When choosing between these two approaches, consider the following factors: * **Readability**: If you need to perform a simple array check, `array.includes()` might be more readable. However, if you're working with objects and property lookups, `obj['sausage']` can be more concise. * **Performance**: For large arrays or complex searches, `array.includes()` might be slower than `obj['sausage']`. However, for most use cases involving simple array checks, the performance difference is unlikely to be significant. **Library Usage** The test case uses a JavaScript library called `Object.keys()`, which returns an array of strings containing the property names of an object. The purpose of this library is to provide a convenient way to iterate over an object's properties and perform operations on them. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you want to test other approaches for array checks, consider the following alternatives: 1. **`array.indexOf('sausage')`**: Similar to `includes()`, but returns an index instead of a boolean value. 2. **`array.includes.call(array, 'sausage')`**: A more explicit way to call the `includes()` method on the array object. 3. **`forEach()`**: Iterates over the array using a callback function, which can be used to perform operations on each element. By testing these alternative approaches, you can gain insights into their performance and readability compared to the original `array.includes()` method.
Related benchmarks:
array includes vs object key
array includes (worst case) vs object key
Object.keys(object).includes(key) vs key in object
array includes vs object key -1
Comments
Confirm delete:
Do you really want to delete benchmark?