var appState = "active";
var regex = /inactive|background|other|another/;
var arr = ['inactive', 'background', 'other', 'another'];
regex.test(appState);
arr.includes(appState);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
Array.includes |
Test name | Executions per second |
---|---|
RegEx.test | 13898367.0 Ops/sec |
Array.includes | 14863528.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is comparing two approaches: using a regular expression (regex.test
) and using an array's includes()
method to check if a specific string exists in the array.
Options Compared
There are four options being compared:
includes()
method of an array to check if a specific value exists in the array.Regex.test
.===
).Pros and Cons
Here's a brief overview of each approach:
^
and $
) and word boundaries.String.indexOf()
or simple string comparison. Also, it's more geared towards searching values in an array rather than performing a direct string comparison.Array.includes()
, but optimized for strings instead of arrays.Array.includes()
.Library Usage
There is no explicit library usage in this benchmark, aside from the built-in JavaScript functions (regex.test
, Array.includes()
, and String.indexOf()
).
Special JS Features/Syntax
None mentioned. The code uses standard JavaScript syntax and features.
Other Alternatives
Some alternative approaches could be:
for
loop or forEach
to iterate over the array and check for the presence of the value.However, these alternatives are not explicitly mentioned in the benchmark definition.