Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx vs Array.includes
(version: 0)
Comparing performance of:
RegEx.test vs Array.includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var regex = /1/; var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
Tests:
RegEx.test
arr.forEach(el => regex.test(el));
Array.includes
arr.includes('1');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
Array.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
7449746.0 Ops/sec
Array.includes
85070064.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Regular Expressions (RegExp)**: * Purpose: Regular expressions are a powerful way to match patterns in strings. They're often used for string validation, extraction, and manipulation. * Pros: + Can be very flexible and powerful for specific use cases + Often faster than other string searching methods when dealing with complex patterns * Cons: + Can be slower than other methods when dealing with simple searches + More error-prone due to the complexity of regex syntax 2. **Array.includes()**: * Purpose: `includes()` is a method that checks if an array includes a specific value. * Pros: + Faster and more efficient for simple string searches + Less prone to errors compared to RegExp * Cons: + May not be as flexible or powerful as RegExp for complex searches **Other Considerations** When choosing between RegExp and `includes()`, consider the following: * **Complexity of search**: If you're searching for a simple pattern, `includes()` might be faster. However, if your search is more complex, RegExp might be a better choice. * **Error handling**: RegExp can be error-prone due to its complex syntax. Make sure to handle errors properly when using RegExp. **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: 1. `arr.forEach(el => regex.test(el));` 2. `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: * **String.prototype.indexOf()**: A built-in JavaScript method that searches for a specific value in a string. * **String.prototype.split()**: Can be used to search for a pattern and extract matches, but it might not be as efficient as RegExp or `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!
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
array.toString vs `${array}`
array.splice vs array.length
DTMF: array includes vs regex
array.length = 0 vs []
Comments
Confirm delete:
Do you really want to delete benchmark?