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)
return a.includes('mx')
return b.has('mx')
return a.indexOf('mx') >= 0
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup | |
indexof |
Test name | Executions per second |
---|---|
includes | 20169352.0 Ops/sec |
lookup | 1365044736.0 Ops/sec |
indexof | 25141462.0 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
The benchmark compares the performance of three different methods for checking if a value exists within an array or set:
array.includes(value)
: checks if a value is present in the array using the includes()
method.set.has(value)
: checks if a value is present in the Set data structure using the has()
method.array.indexOf(value)
(or, equivalently, value >= 0 && array.includes(value)
) : checks if an index exists for a value in the array by attempting to find its position.Options Compared
The three options are compared:
array.includes(value)
: uses the includes()
method of arrays.set.has(value)
: uses the has()
method of Sets.array.indexOf(value)
: uses the indexOf()
method of arrays (or an equivalent implementation using includes() && value >= 0
).set.has()
since it requires traversing the array from start to end.Library Used
The benchmark uses JavaScript's built-in Set data structure. The Set data structure is a collection of unique values that can be used for efficient membership testing (i.e., checking if a value exists within the set).
Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax beyond what's required to implement the three options being compared.
Other Alternatives
Other alternatives for these methods include:
includes()
with an array filter:
array.filter(value => value === target).length > 0`indexOf()
since it doesn't require traversing the entire array.set.has()
due to the need to create a new filtered array.in
operator: target in array || set.has(target)
It's worth noting that the choice of method will depend on the specific requirements of your use case, such as the size of the dataset, the available resources (e.g., CPU power, memory), and the trade-offs between simplicity, performance, and code readability.