Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (string input)
(version: 0)
Comparing performance of:
includes vs has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['emre', 'laiq', 'dan', 'luka', 'madhav', 'anant']; var b = new Set(a)
Tests:
includes
return a.includes('luka')
has
return b.has('luka')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
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):
**What is tested:** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark tests two approaches to check if a specific string ("luka") exists in an array (`a`) or a Set object (`b`). **Options compared:** Two options are compared: 1. **Array `includes()` method**: This method checks if the specified string ("luka") exists in the array (`a`). 2. **Set `has()` method**: This method checks if the specified value ("luka") exists in the Set object (`b`) created from the array (`a`). **Pros and Cons:** * **Array `includes()` method**: + Pros: - Wide support across browsers and platforms. - Simple and intuitive to use. + Cons: - Can be slower than other methods for large arrays, as it needs to iterate through all elements. - May not be suitable for very small arrays or Sets, as it can still perform unnecessary work. * **Set `has()` method**: + Pros: - Generally faster than the array `includes()` method, especially for large sets. - Can be more efficient for small sets or arrays, as it uses a hash table lookup. + Cons: - May not be supported by older browsers or platforms that don't support Sets. - Requires creating an empty Set object (`b`) from the array (`a`). **Library:** In this benchmark, no external library is used. However, a Set object (`b`) is created from the array (`a`). A Set in JavaScript is a collection of unique values, and it's implemented as a hash table for efficient lookups. **Special JS feature or syntax:** The use of Sets in this benchmark is an example of a modern JavaScript feature. Sets were introduced in ECMAScript 2015 (ES6) and have since become a standard part of the language. **Other alternatives:** If you don't want to use the `includes()` method, you can also consider using other approaches, such as: * **Using `indexOf()`**: This method returns the index of the specified value in the array. If the value is not found, it returns -1. * **Using a `for` loop**: You can iterate through the array and check each element manually to see if it matches the specified string. However, these alternatives may have different performance characteristics than the Set `has()` method or the array `includes()` method.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?