var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = new Set(a);
var c = Object.fromEntries(a.map(e => [e, true]));
return a.includes(3)
return b.has(3)
return c[3]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes | |
Set.has | |
Object access |
Test name | Executions per second |
---|---|
Array.includes | 27648690.0 Ops/sec |
Set.has | 38309336.0 Ops/sec |
Object access | 36524808.0 Ops/sec |
The benchmark outlined in the provided JSON compares three different methods for checking the presence of an element in a collection: using an array with the Array.includes()
method, using a Set
with the Set.has()
method, and using a plain object for direct property access. The main goal is to evaluate the performance of these approaches in terms of execution speed.
Array.includes()
true
if the value is found, and false
otherwise.Set.has()
Set
, which is a collection of unique values.Set
is a separate object.Object Access
true
value signifies the presence of each element.Set
.Set
for checking membership, as it diverges from the typical collection model in JavaScript.Set
and object access may use more memory than arrays due to their internal structures. This should be considered when working with numerous elements.The results indicate the number of executions per second for each method on a specific test using Chrome on Windows.
This suggests that using Set
is the most efficient for element presence checks in terms of execution speed, followed closely by direct object access, with the array method being the least performant.
Besides the methods tested, alternatives could include:
In conclusion, the benchmark provides clear insights into the performance implications of element presence checks in JavaScript collections, helping developers choose the most appropriate method based on their specific needs.