var a = [
'height',
'width',
'maxHeight',
'maxWidth',
'maxHeight',
'minWidth',
'color',
'bg',
'backgroundColor',
'opacity',
'm',
'mt',
'mb',
'mr'
];
var b = new Set(a)
var c = a.slice();
c.push('abc','def','ghi','other word', 'notinlist','123','4334232313','borderwersese');
return a.includes(c[Math.floor(Math.random() * c.length)])
return b.has(c[Math.floor(Math.random() * c.length)])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
arr.includes | |
set.has |
Test name | Executions per second |
---|---|
arr.includes | 1843716.4 Ops/sec |
set.has | 1855351.9 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and other considerations.
Benchmark Definition
The benchmark is designed to compare two JavaScript methods: array.includes()
and Set.has()
. The test aims to measure which method is faster for adding a string that is not in the list.
Script Preparation Code
The script prepares two arrays:
a
: an array of strings, including some duplicates.c
: a copy of a
with additional random strings added to it, including one string that is not in the original array ('notinlist'
).A new Set object b
is created from the elements of array a
.
Html Preparation Code
There is no HTML preparation code provided.
Individual Test Cases
The benchmark consists of two test cases:
arr.includes
: This test case uses the array.includes()
method to check if a random string from array c
is present in array a
.set.has
: This test case uses the Set.has()
method to check if a random string from array c
is present in Set object b
.Options Compared
The two methods are compared:
array.includes()
Set.has()
Pros and Cons of Each Approach
Other Considerations
Set.has()
might benefit from cache locality due to its hash table implementation.Library/Module Used
The benchmark uses the built-in Array
and Set
objects, which are part of the ECMAScript standard. These objects provide optimized implementations for array and set operations, respectively.
Special JS Feature/Syntax
There is no specific JavaScript feature or syntax used in this benchmark that requires special explanation. The focus is on comparing two existing methods: array.includes()
and Set.has()
.