Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array include vs set has
(version: 0)
array include vs set has
Comparing performance of:
// array .includes vs // set .has vs // array .some(!include) vs // set .some(!has)
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var scopes = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar.settings.readonly https://www.googleapis.com/auth/calendar openid https://www.googleapis.com/auth/calendar.events".split(' '); var requiredScopes = [ 'yasss', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events', 'https://www.googleapis.com/auth/calendar.settings.readonly', ];
Tests:
// array .includes
let scopesOk = true; for (let i = 0, length = requiredScopes.length; i < length; ++i) { if (!scopes.includes(requiredScopes[i])) { // console.log("early break"); scopesOk = false; break; } // console.log(i) }
// set .has
let scopesOk = true; const scopeSet = new Set(scopes); for (let i = 0, length = requiredScopes.length; i < length; ++i) { if (!scopeSet.has(requiredScopes[i])) { // console.log("early break"); scopesOk = false; break; } }
// array .some(!include)
let scopesOk = requiredScopes.some(scope => !scopes.includes(scope))
// set .some(!has)
const scopeSet2 = new Set(scopes); let scopesOk = requiredScopes.some(scope => !scopeSet2.has(scope))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
// array .includes
// set .has
// array .some(!include)
// set .some(!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):
I'd be happy to help you understand the JavaScript microbenchmark being tested on MeasureThat.net. **Benchmark Overview** The benchmark tests three different approaches for checking if an element is present in an array: using `Array.prototype.includes()`, using `Array.prototype.some()` with a negative predicate, and using Sets (`Set.prototype.has()`). The goal is to determine which approach is the fastest. **Options Compared** 1. **`Array.prototype.includes()`**: This method checks if a specified value (in this case, an element of the `requiredScopes` array) exists in the given array. 2. **`Array.prototype.some()` with negative predicate**: In this approach, the `some()` method is used to check if at least one element of the array does not satisfy a certain condition (i.e., includes the value). The predicate function takes three arguments: the current element, the index, and the array itself. 3. **`Set.prototype.has()`**: This method checks if an element exists in a Set object. **Pros and Cons** 1. `Array.prototype.includes()`: * Pros: widely supported, easy to use, and fast for small arrays. * Cons: O(n) time complexity, which can be slow for large arrays. 2. `Array.prototype.some()` with negative predicate: * Pros: allows for early termination if the condition is false for all elements, and can be faster than `includes()` for very large arrays. * Cons: less intuitive usage, requires creating a predicate function, and can be slower due to the additional overhead of the `some()` method. 3. `Set.prototype.has()`: * Pros: fast for small Sets, as lookups are O(1) on average. * Cons: not designed for this specific use case (checking membership in an array), may not work correctly if the Set is not initialized with all elements. **Additional Considerations** The benchmark also takes into account the following: * The `scopes` variable is a string containing multiple scope values, which are split into an array using the `split()` method. * The `requiredScopes` array contains specific scope values that need to be checked for membership in the `scopes` array. **Library and Purpose** In this benchmark, no specific JavaScript library is used. However, it's worth noting that some libraries (e.g., Lodash) provide optimized implementations of these methods or offer alternative approaches. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. **Alternatives** Other alternatives for checking membership in an array could include: * Using `Array.prototype.findIndex()` with a negative value (i.e., `-1` as the return value) * Creating a custom implementation using bitwise operations * Using a library like Lodash, which provides optimized implementations of these methods Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.
Related benchmarks:
Nested vs Cascade
JSON.parse - aggregated events vs single events
Find events real end
Shiogui - Push x Reduce Logged
Comments
Confirm delete:
Do you really want to delete benchmark?