var regex = /1/;
var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
arr.forEach(el => regex.test(el));
arr.includes('1');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
Array.includes |
Test name | Executions per second |
---|---|
RegEx.test | 2078449.5 Ops/sec |
Array.includes | 24233454.0 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Definition
The benchmark is comparing two approaches: using regular expressions (RegExp
) for string searching, specifically with the test()
method, versus using the includes()
method on an array. The benchmark name is "RegEx vs Array.includes".
Options Compared
We have two options being compared:
includes()
is a method that checks if an array includes a specific value.Other Considerations
When choosing between RegExp and includes()
, consider the following:
includes()
might be faster. However, if your search is more complex, RegExp might be a better choice.Library and Purpose
There is no specific library mentioned in the benchmark definition. However, includes()
method is a built-in JavaScript method that's available on arrays (and some other data structures).
Special JS Feature or Syntax
Neither of the options requires special JavaScript features or syntax beyond what's built into the language.
Now, let's look at the test cases:
arr.forEach(el => regex.test(el));
arr.includes('1');
Both test cases are using the same array and RegExp instance. The first test case uses a loop to iterate over the array and apply the test()
method on each element, while the second test case directly checks if '1' is included in the array.
Other Alternatives
If you're looking for alternative approaches, consider:
includes()
.Keep in mind that the choice of approach depends on your specific use case and requirements. MeasureThat.net helps you compare performance between different approaches, so feel free to experiment and find the best solution for your needs!