var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20];
var b = new Set(a)
return a.includes(9)
return b.has(9)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 87489672.0 Ops/sec |
lookup | 186865072.0 Ops/sec |
Let's dive into explaining the benchmark and its various components.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, specifically comparing two approaches for checking if an element exists in an array: array.includes()
and set.has()
. This benchmark is designed to measure the performance difference between these two methods in a v22 version of JavaScript.
Script Preparation Code
The script preparation code creates two variables:
a
: An array of 20 elements, containing numbers from 1 to 20.b
: A new Set object created from the elements of array a
. Sets are a data structure in JavaScript that store unique values.var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20];
var b = new Set(a);
Html Preparation Code
There is no HTML preparation code provided for this benchmark.
Test Cases
The test cases are defined in the "Individual test cases" array:
**: This test case uses the
array.includes()method to check if an element (9) exists in the original array
a`. The script will return a boolean value indicating whether 9 is present or not.**: Similar to the first test case, this one uses the
set.has()method on the Set object
b` created earlier to check if the same element (9) exists.Libraries and Features
Set
data structure is a built-in JavaScript library for storing unique values. It's used in both test cases.Options Compared
The benchmark tests the performance of two options:
array.includes()
: Checks if an element exists in the array by iterating through its elements.set.has()
: Checks if an element exists in a Set data structure by using a hash-based lookup.Pros and Cons of Each Approach
array.includes()
: Pros:set.has()
: Pros:Other Alternatives
If you need a different approach or performance optimization, consider:
Map
or Set-like objects
, which might offer better performance in specific use cases.Keep in mind that the choice of approach depends on your specific requirements, data size, and performance constraints.