Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (random string values), add strings not in list
(version: 0)
Comparing performance of:
arr.includes vs set.has
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [ 'height', 'width', 'maxHeight', 'maxWidth', 'maxHeight', 'minWidth', 'color', 'bg', 'backgroundColor', 'opacity', 'm', 'mt', 'mb', 'mr', 'mr', 'mx', 'my', 'p', 'pt', 'pb', 'pr', 'pl', 'px', 'py', 'border', 'boxShadow', 'flex', 'verticalAlign', 'textAlign', 'overflow', 'display', 'cursor' ]; var b = new Set(a) var c = a.slice(); c.push('abc','def','ghi','other word', 'notinlist','123','4334232313','borderwersese');
Tests:
arr.includes
return a.includes(c[Math.floor(Math.random() * c.length)])
set.has
return b.has(c[Math.floor(Math.random() * c.length)])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
arr.includes
set.has
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 the provided JSON benchmark definition. **Benchmark Description:** The benchmark is comparing two approaches to check if an element exists in an array or a Set data structure: 1. `array.includes(c[Math.floor(Math.random() * c.length)])`: This approach uses the built-in `includes` method of JavaScript arrays. 2. `b.has(c[Math.floor(Math.random() * c.length)])`: This approach uses the `has` method of JavaScript Sets. **Options Compared:** * **Array.includes**: This option uses the built-in array method to check if an element exists in the array. * **Set.has**: This option uses the built-in set method to check if an element exists in the Set. **Pros and Cons:** * `array.includes`: + Pros: - Wide browser support (except older versions of Internet Explorer) - Fast, as it's implemented in native code + Cons: - May be slower than other options for very large arrays due to the overhead of the JavaScript engine - Can throw an error if the array is not initialized properly * `Set.has`: + Pros: - Generally faster than `array.includes`, as it's implemented in native code and optimized for hash tables - More memory-efficient, as it only stores unique elements + Cons: - Limited browser support (only modern browsers that support Sets) - Requires initialization of the Set with a set of unique elements **Library/Functionality:** The `Set` data structure is a built-in JavaScript object that allows you to store unique elements. It's implemented as a hash table, which makes lookups and insertions very efficient. **Special JS Feature/Syntax:** This benchmark does not use any special JavaScript features or syntax. **Benchmark Preparation Code Explanation:** The script preparation code creates two arrays: * `a`: An array of string values that will be used to initialize the Set. * `b`: A new Set created from the array `a`. The script also generates a random string value and adds it to the end of array `c` for testing. **Alternative Approaches:** Other approaches could be: * Using the `Array.prototype.forEach` method to iterate over the array and check if the element exists. * Using a custom implementation, such as a loop that checks each element in the array. * Using a different data structure, such as an object or a map. However, these alternatives may not be as efficient or accurate as using the built-in `includes` method for arrays or the `has` method for Sets.
Related benchmarks:
set.has vs. array.includes (string values)
set.has vs. array.includes (random string values)
set.has vs. array.includes (random string values), add strings not in list, smaller list
set.has vs. array.includes vs array.indexOf (string values) - longer
Comments
Confirm delete:
Do you really want to delete benchmark?