Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match 1
(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 = "336456,Gfdfg,Rrtert,345yrt-80fd-41f3-y5rg-er6utyj7kty,7/3/2002"; var regex = /[0-9a-f]/;
Tests:
RegEx.test
regex.test(string);
String.includes
!string.includes("-");
String.match
!string.match("-");
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for searching a string for a specific pattern: 1. `String.includes()` 2. `RegExp.test()` 3. `String.match()` These functions are part of JavaScript's built-in String and RegExp objects, which allow developers to search for patterns in strings. **Options Compared** The benchmark is comparing the performance of these three approaches on the same input string: * `string = "336456,Gfdfg,Rrtert,345yrt-80fd-41f3-y5rg-er6utyj7kty,7/3/2002"` * `regex = /[0-9a-f]/` The pattern being searched for is a hexadecimal value (`[0-9a-f]`) contained within the input string. **Pros and Cons of Each Approach** Here's a brief summary of each approach: 1. **String.includes()** * Pros: Simple, easy to use, and often faster than RegExp. * Cons: Can be slower for large strings or complex patterns. 2. **RegExp.test()** * Pros: More flexible and powerful than String.includes(), especially for complex patterns. * Cons: Can be slower due to the overhead of compiling a regular expression. 3. **String.match()** * Pros: Similar performance to RegExp.test() but with a more straightforward API. * Cons: Less flexible than RegExp.test(). **Library and Special JS Features** None of these approaches rely on specific libraries or special JavaScript features beyond the standard String and RegExp objects. **Benchmark Results** The latest benchmark results show that: * `String.includes()` is the fastest approach, with an execution rate of 16048341.0 executions per second. * `RegExp.test()` is slower than `String.includes()`, but still faster than `String.match()`, with an execution rate of 7302165.5 executions per second. * `String.match()` is the slowest approach, with an execution rate of 4268406.0 executions per second. **Other Alternatives** If you're looking for alternative approaches, here are a few options: 1. **Using a library like Lodash**: Lodash provides a function called `includes()`, which can be faster than the built-in `String.includes()` approach. 2. **Using a regular expression engine**: Engines like RegEx.js or regex-execution provide optimized regular expression execution, which might outperform `RegExp.test()`. 3. **Using a custom implementation**: Depending on your specific use case, you might be able to optimize the search process by using a custom implementation that bypasses JavaScript's built-in string and RegExp functions. Keep in mind that these alternatives may introduce additional complexity or dependencies, so it's essential to consider your project's requirements and constraints before choosing an alternative approach.
Related benchmarks:
RegEx.test vs String.includes vs String.indexOf
Case insensitive RegEx.test vs. String.includes when string doesn’t match
RegEx.matchAll vs includes no match
RegEx.matchAll vs includes with match
test regex vs regex vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?