Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs object key with random search value
(version: 0)
performance comparison of ways to find if an array contains a value
Comparing performance of:
Includes vs Object[key] vs Set.has
Created:
2 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 the_key = Math.random() + '' obj[the_key] = Math.random() + '' var array = Object.keys(obj); array.sort(); var set = new Set(array);
Tests:
Includes
array.includes(the_key)
Object[key]
obj[the_key]
Set.has
set.has(the_key)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Includes
Object[key]
Set.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
467307.0 Ops/sec
Object[key]
6603126.0 Ops/sec
Set.has
6823655.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition JSON** The JSON represents a JavaScript benchmark that tests three different ways to check if an array contains a specific value: 1. **Array.includes()**: This method checks if a given element is present in an array. 2. **Object[key]**: This approach involves accessing a property of an object using the bracket notation, where `key` is the index of the desired element in the object's keys. 3. **Set.has()**: This method checks if a specific value is present in a Set data structure. **Benchmark Preparation Code** The script preparation code generates a large array (1000 elements) and populates it with random values. It then selects a random key from this array, assigns a random value to the corresponding property of an object, sorts the object's keys, and creates a new Set containing these keys. **Options Compared** Here are the three options being compared: 1. **Array.includes()**: This approach is generally the most efficient way to check for presence in an array. * Pros: Fast, widely supported, and easy to implement. * Cons: Can be slower than other approaches for very large arrays due to JavaScript's caching behavior. 2. **Object[key]**: This approach involves accessing a property of an object using the bracket notation. * Pros: May be faster than Array.includes() for small arrays or when working with objects that have a large number of keys. * Cons: Can be slower for very large arrays, and its performance can degrade due to JavaScript's caching behavior. Additionally, this approach requires the array's length to match the object's key count, which may not always be the case. 3. **Set.has()**: This method checks if a specific value is present in a Set data structure. * Pros: Can be faster than Array.includes() for very large arrays or when working with Sets, as Sets use hash tables internally for efficient lookups. * Cons: May require additional memory to store the Set and its elements. Additionally, this approach requires JavaScript's built-in Set object. **Library/External Functionality** There is no explicit library being used in this benchmark. However, it relies on JavaScript's built-in data structures: 1. **Array**: Used for storing the array of random values. 2. **Object**: Used to store the properties with keys generated from the sorted array. 3. **Set**: Used to create a Set containing the object's keys. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. However, it does utilize: 1. **Template literals**: Used to generate random strings for the `obj` and key properties. 2. **Regular expressions**: Not explicitly used but implicit due to the `Math.random()` function calls. **Alternatives** Other alternatives could be explored, such as: 1. Using a library like Lodash or Ramda for array operations. 2. Implementing a custom search algorithm using bitwise operations or hashing functions. 3. Comparing with other data structures like linked lists or trees. Keep in mind that the choice of alternative options would depend on specific requirements and constraints, such as performance, memory usage, or complexity.
Related benchmarks:
Set has vs object key
Object.keys(object).includes(key) vs key in object
array includes vs object key vs set
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?