Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes (worst case) 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; }, { }); var array = Object.keys(obj); obj['sausage'] = 'tst'; array.push('sausage');
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:
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):
I'll break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Definition** The benchmark measures the performance of two approaches to check if an array contains a value: 1. `array.includes('sausage')` 2. `obj['sausage']` Both approaches aim to find if the string "sausage" is present in the array or object respectively. **Script Preparation Code** The script preparation code generates a large array of 1000 objects, each containing random properties with keys and values. The code then: * Creates an array from the object's keys (`Object.keys(obj)`). * Sets a property `sausage` on the original object. * Pushes the string "sausage" onto the end of the array. This setup simulates a scenario where you might need to search for a specific value in an array or object, which is what the benchmark tests. **Options Compared** The two approaches being compared are: 1. `array.includes('sausage')`: This method checks if the string "sausage" is present in the array by iterating over its elements and checking for a match. 2. `obj['sausage']`: This approach uses object key access to retrieve the value associated with the key "sausage". **Pros and Cons** ### Array Inclusion (`array.includes('sausage')`) Pros: * Simple and straightforward implementation. * Fast lookup time, as it only requires a single pass through the array. Cons: * May have performance issues if the array is very large or if the search value is not present in the array (worst-case scenario). * Requires iteration over the entire array, which can be slow for large datasets. ### Object Key Access (`obj['sausage']`) Pros: * Fast lookup time, as it directly accesses the object's property. * May be more efficient than iterating over an array. Cons: * Assumes that the key exists in the object; if not, it will result in a TypeError or undefined value. * Requires access to the object's properties, which may not always be possible or convenient. **Library and Special JS Features** In this benchmark, the `Object.keys()` method is used, which is a built-in JavaScript function that returns an array of an object's own enumerable property names. This is not a library per se but rather a native JavaScript feature. There are no special JavaScript features mentioned in the provided code or benchmark definition. **Other Alternatives** If you're interested in exploring alternative approaches to this problem, here are a few examples: * Using `Set` data structure: You can create a set from the array and check for membership using the `has()` method. This approach is generally faster than iterating over an array. * Using `Map` data structure: Similar to sets, you can use a map to store unique values and then search for membership. Keep in mind that these alternatives might not be directly applicable to this specific benchmark or may have different performance characteristics depending on the use case.
Related benchmarks:
array includes 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?