var regex = /\d/;
var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
regex.test('1')
arr.includes('1');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
Array.includes |
Test name | Executions per second |
---|---|
RegEx.test | 18800204.0 Ops/sec |
Array.includes | 24099810.0 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and considered.
Benchmark Overview
The benchmark is designed to compare the performance of two JavaScript methods: regular expression (RegExp) test()
and array includes(v2)
for searching a specific value ("1") within an array of digits. The goal is to determine which method is faster.
Comparison Options
There are only two options being compared:
test()
)includes(v2)
Other Considerations
v2
in arr.includes()
, which refers to the second version of the includes()
method, introduced in ECMAScript 2019 (ES10). This is not a special feature or syntax, but rather a specific implementation detail.Benchmark Preparation Code
The provided script preparation code sets up the necessary variables:
regex
: Creates a new RegExp object with a pattern that matches any digit (\\d
).arr
: Defines an array of digits ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
).Individual Test Cases
Each test case consists of a single benchmark definition, which is executed once:
RegEx.test
, calls the RegExp test()
method on the prepared regex object with the value '1'
.Array.includes
, calls the array includes()
method (specifically, version 2) on the prepared array with the same value '1'
.Latest Benchmark Result
The provided result shows the execution statistics for each test case across different devices and browsers. The results indicate that Array.includes(v2)
is slightly faster than RegEx.test()
, but the difference is relatively small.
Keep in mind that these results may not be representative of your specific use cases or environments, as they are based on a single set of device and browser configurations.