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',
];
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)
}
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;
}
}
let scopesOk = requiredScopes.some(scope => !scopes.includes(scope))
const scopeSet2 = new Set(scopes);
let scopesOk = requiredScopes.some(scope => !scopeSet2.has(scope))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
// array .includes | |
// set .has | |
// array .some(!include) | |
// set .some(!has) |
Test name | Executions per second |
---|---|
// array .includes | 12679112.0 Ops/sec |
// set .has | 570935.2 Ops/sec |
// array .some(!include) | 9837363.0 Ops/sec |
// set .some(!has) | 616316.4 Ops/sec |
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
Array.prototype.includes()
: This method checks if a specified value (in this case, an element of the requiredScopes
array) exists in the given array.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.Set.prototype.has()
: This method checks if an element exists in a Set object.Pros and Cons
Array.prototype.includes()
:Array.prototype.some()
with negative predicate:includes()
for very large arrays.some()
method.Set.prototype.has()
:Additional Considerations
The benchmark also takes into account the following:
scopes
variable is a string containing multiple scope values, which are split into an array using the split()
method.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:
Array.prototype.findIndex()
with a negative value (i.e., -1
as the return value)Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.