Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match1
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello, world!"; var regex = /[,]/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("Hello");
String.match
string.match("Hello");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
4697916.0 Ops/sec
String.includes
10975197.0 Ops/sec
String.match
2754317.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The test is designed to compare the performance of three different approaches for finding a substring in a string: 1. `String.includes()` 2. `RegExp.test()` (RegEx) 3. `String.match()` with a capturing group (`String.match()`) **Approaches Compared** Here's a brief overview of each approach and their characteristics: ### 1. `String.includes()` `String.includes()` is a simple method that checks if a string contains another substring. It returns `true` if the substring is found, and `false` otherwise. Pros: Simple, efficient, and widely supported. Cons: Can be slower than other approaches for large substrings or complex searches. ### 2. `RegExp.test() (RegEx)` `RegExp.test()` (or `RegEx.test()`) is a method that tests if a string matches a regular expression pattern. It returns an object with information about the match, including its index in the string. Pros: Flexible and powerful for complex searches, but can be slower than other approaches. Cons: Requires more code and can be overkill for simple substring searches. ### 3. `String.match()` with a capturing group (`String.match()`)` `String.match()` is a method that finds the first match of a regular expression pattern in a string. When used with a capturing group, it returns an array containing the matched text and its index in the string. Pros: Similar to `RegExp.test()`, but returns an array instead of an object. Cons: Can be slower than other approaches for simple searches. **Library Used** In this benchmark, none of the libraries are explicitly mentioned. However, `RegExp` is a built-in JavaScript library that provides regular expression functionality. **Special JS Feature or Syntax** None are used in this benchmark. **Other Alternatives** Some alternative approaches to searching substrings in JavaScript include: * Using a `for...loop` with indexing: `string.indexOf('Hello')` * Using the `Array.prototype.indexOf()` method: `[string].indexOf('Hello')` * Using other libraries like `jsregex` or `regexlib` **Benchmark Test Cases** The benchmark test cases are simple string literals that contain the substring to be searched for (`"Hello, world!"`). The test cases use different approaches: 1. `String.includes()`: `"string.includes("Hello");"` 2. `RegExp.test() (RegEx)`: `"regex.test(string);"` 3. `String.match()` with a capturing group: `"string.match("Hello")"` **Benchmark Results** The benchmark results show the execution count per second for each approach on a desktop Chrome 126 browser: | Test Name | Executions Per Second | | --- | --- | | String.includes | 4697916.0 | | RegEx.test (RegEx) | 10975197.0 | | String.match | 2754317.75 | The results suggest that `RegExp.test()` is the fastest approach, followed by `String.includes()`.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs String.includes
RegEx.test (with inline regex) vs. String.includes vs. String.match
Long regex test vs string includes
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?