var a = [
'height',
'width',
'maxHeight',
'maxWidth',
'maxHeight',
'minWidth',
'color',
'bg',
'backgroundColor',
'opacity',
'm',
'mt',
'mb',
'mr',
'mr',
'mx',
'my',
'p',
'pt',
'pb',
'pr',
'pl',
'px',
'py',
'border',
'boxShadow',
'flex',
'verticalAlign',
'textAlign',
'overflow',
'display',
'cursor'
];
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 | 1593768.0 Ops/sec |
set.has | 1651396.5 Ops/sec |
Let's break down the provided JSON benchmark definition.
Benchmark Description: The benchmark is comparing two approaches to check if an element exists in an array or a Set data structure:
array.includes(c[Math.floor(Math.random() * c.length)])
: This approach uses the built-in includes
method of JavaScript arrays.b.has(c[Math.floor(Math.random() * c.length)])
: This approach uses the has
method of JavaScript Sets.Options Compared:
Pros and Cons:
array.includes
:Set.has
:array.includes
, as it's implemented in native code and optimized for hash tablesLibrary/Functionality:
The Set
data structure is a built-in JavaScript object that allows you to store unique elements. It's implemented as a hash table, which makes lookups and insertions very efficient.
Special JS Feature/Syntax: This benchmark does not use any special JavaScript features or syntax.
Benchmark Preparation Code Explanation:
The script preparation code creates two arrays:
a
: An array of string values that will be used to initialize the Set.b
: A new Set created from the array a
.The script also generates a random string value and adds it to the end of array c
for testing.
Alternative Approaches: Other approaches could be:
Array.prototype.forEach
method to iterate over the array and check if the element exists.However, these alternatives may not be as efficient or accurate as using the built-in includes
method for arrays or the has
method for Sets.