Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (string values)
(version: 0)
Comparing performance of:
includes vs lookup
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:
includes
return a.includes('mx')
lookup
return b.has('mx')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
21 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
53792552.0 Ops/sec
lookup
206979600.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark compares the performance of two approaches to check if a specific string value exists in an array and a Set data structure, respectively. The test cases are designed to measure the execution speed of these operations for various strings from a predefined list `a`. **Approaches Compared** Two approaches are compared: 1. **`includes` method on an array**: This approach uses the built-in `includes()` method on the array `a`. It iterates over the elements of the array until it finds the target string `'mx'`. 2. **`has` method on a Set**: This approach uses the built-in `has()` method on the Set `b`, which is created from the same list `a`. **Pros and Cons of Each Approach** **`includes` Method on an Array:** Pros: * Widely supported in modern browsers and JavaScript engines. * Easy to use and understand. Cons: * Requires iterating over the array elements, which can be slower for large arrays. * May not perform well if the target string is near the beginning of the array (due to the "linear search" behavior). **`has` Method on a Set:** Pros: * Has an average-case time complexity of O(1), making it very efficient for lookups. * Can be faster than `includes()` method, especially for large datasets. Cons: * May not work well in older browsers or JavaScript engines that do not support Sets. * Requires creating a new data structure (the Set) to store the array elements. **Library Used** The Set library is used in this benchmark. A Set is a collection of unique values, which is implemented using a hash table internally. The `has()` method on a Set checks if a specific value exists in the set by looking up the key in the hash table. **Special JavaScript Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark that would require additional explanation. However, it's worth noting that modern browsers and JavaScript engines have optimized implementations of these methods (e.g., `includes()` and `has()`) that can significantly impact performance.
Related benchmarks:
set.has vs. array.includes vs array.indexOf (string values)
set.has vs. array.includes (random 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?