Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (random string values)
(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)
Tests:
arr.includes
return a.includes(a[Math.floor(Math.random() * a.length)])
set.has
return b.has(a[Math.floor(Math.random() * a.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):
**Benchmark Explanation** The provided benchmark compares the performance of two JavaScript methods: `array.includes()` and `set.has()`. The test is designed to measure which method is faster when searching for an element in an array or a set, respectively. **Options Compared** Two options are compared: 1. **Array.includes()**: This method checks if a specified value exists in the array. 2. **Set.has()**: This method checks if a specified value exists in the set. **Pros and Cons of Each Approach** * **Array.includes()**: + Pros: widely supported, fast for small arrays, easy to implement. + Cons: can be slow for large arrays (O(n) complexity), may not be suitable for sets or other data structures with unique values. * **Set.has()**: + Pros: faster for sets and other data structures with unique values, O(1) complexity on average. + Cons: less widely supported, requires a set data structure. **Library Used** The `Set` object is used in the benchmark. A set is an unordered collection of unique values, which makes it suitable for this test case. **Special JS Feature or Syntax** None mentioned in this specific test case. **Other Considerations** This benchmark is relevant for developers working with JavaScript and may be useful when deciding between `array.includes()` and `set.has()` for performance-critical applications. Additionally, understanding the trade-offs between these two methods can help optimize code for different use cases. **Alternatives** Some alternative approaches to this test case might include: 1. **Array.prototype.find()**: This method returns the first element in the array that satisfies the provided condition. 2. **Map.has()**: If using a `Map` data structure instead of a set, this method checks if a specified value exists in the map. These alternatives would require modifications to the benchmark test case and may not directly compare to the original test.
Related benchmarks:
set.has vs. array.includes (string values)
set.has vs. array.includes vs array.indexOf (string values)
set.has vs. array.includes (random string values), add strings not in list
set.has vs. array.includes vs array.indexOf (string values) - longer
Comments
Confirm delete:
Do you really want to delete benchmark?