Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regexp test VS array.every and includes
(version: 0)
Comparing performance of:
Includes vs Regex.test
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const keywords = ['some', 'strings']; const regex = new RegExp(keywords.join("|"), "i"); const string = "Some guitars don't have a lot of strings on them.";
Tests:
Includes
const keywords = ['some', 'strings']; const string = "Some guitars don't have a lot of strings on them."; keywords.every(str => string.toLowerCase().includes(str.toLowerCase()));
Regex.test
const keywords = ['some', 'strings']; const regex = new RegExp(keywords.join("|"), "i"); const string = "Some guitars don't have a lot of strings on them."; regex.test(string);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
Regex.test
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
4484873.0 Ops/sec
Regex.test
2475986.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Description** The benchmark compares two approaches to test whether a string contains specific keywords: using `Array.prototype.every()` and using a regular expression (`RegExp`). The goal is to determine which approach is faster. **Options Compared** There are two main options being compared: 1. **`Array.prototype.every()`**: This method checks if every element in the array passes the provided test function. In this case, it's used to check if each keyword is present in the string (case-insensitive). 2. **Regular Expression (`RegExp`)**: A regular expression can be used to match a pattern in a string. Here, the regular expression is created by joining the keywords with `|` (the "or" operator), and the `i` flag at the end makes it case-insensitive. **Pros and Cons of Each Approach** 1. **`Array.prototype.every()`**: * Pros: + Easy to understand and implement. + Can be more intuitive for developers familiar with array methods. * Cons: + Creates an array of keyword lowercase strings, which can lead to extra overhead. + May not be as efficient for very large arrays or long strings. 2. **Regular Expression (`RegExp`)**: * Pros: + More efficient than `Array.prototype.every()` because it only scans the string once. + Can be more flexible and powerful for pattern matching. * Cons: + Requires a better understanding of regular expressions to implement correctly. + May not be as easy to understand or debug for developers unfamiliar with regex. **Library** There is no explicit library used in this benchmark. However, `Array.prototype.every()` relies on the array data structure, which is a fundamental concept in JavaScript. **Special JS Feature/Syntax** None mentioned explicitly. No special JavaScript features or syntax are being tested in this benchmark. **Other Alternatives** For larger strings or more complex pattern matching, other approaches like using `String.prototype.includes()` with multiple arguments (to check for all keywords at once) might be faster than using regular expressions. Additionally, some libraries like `regex-escape` or `regex-util` provide optimized functions for creating and working with regular expressions. Keep in mind that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
String.match 2
RegEx.test vs. String.includes vs. String.match vs Array.includes
RegExp.test() vs Array.includes()
regex.test vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?