Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.match vs Array.includes
(version: 0)
Comparing performance of:
String.match vs Array.includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var appState = "active"; var regex = /inactive|background/; var arr = ['inactive', 'background'];
Tests:
String.match
appState.match(regex)
Array.includes
arr.includes(appState);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String.match
Array.includes
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/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.match
5220568.0 Ops/sec
Array.includes
5594089.0 Ops/sec
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 test compares two JavaScript methods: `String.match()` and `Array.includes()`. The goal is to determine which method is faster for searching an array or string using regular expressions. **Options Compared** There are two options being compared: 1. **`String.match()`**: This method returns a string containing the matched text, or null if no match is found. It's used to search for a pattern in a string. 2. **`Array.includes()`**: This method returns a boolean indicating whether an element with the specified value exists in the array. **Pros and Cons of Each Approach** 1. **`String.match()`**: * Pros: Can be faster for certain use cases, as it's optimized for regular expression matching. * Cons: May not work correctly for all types of patterns (e.g., non-regex patterns), and can be slower than `Array.includes()` for arrays without a specified value. 2. **`Array.includes()`**: * Pros: Generally faster and more straightforward to use, as it's designed specifically for array searches. * Cons: May not be optimized for regular expression matching, and may not work correctly for certain edge cases. **Other Considerations** In this benchmark, the `String.match()` method is using a simple regular expression (`/inactive|background/`), while the `Array.includes()` method is simply checking if the string "active" exists in the array. This might skew the results, but it's likely that the test is intended to compare the general performance of both methods. **Library Used** There is no explicit library mentioned in the benchmark definition or test cases. However, the use of regular expressions suggests that JavaScript's built-in `RegExp` object is being used. **Special JS Feature or Syntax** None are explicitly mentioned, but it's worth noting that this benchmark uses a specific version of Chrome (112) and its associated JavaScript engine. This might affect the results, as different browsers or versions may have varying performance characteristics for these methods. **Other Alternatives** If you were to rewrite this benchmark, you might consider adding additional test cases or variations, such as: * Using more complex regular expressions * Searching arrays with larger sizes * Using `String.match()` with non-regex patterns (e.g., using a simple string comparison) * Adding error handling or edge case scenarios to the tests Keep in mind that this benchmark is focused on comparing the performance of two specific methods, so adding additional test cases might dilute the focus.
Related benchmarks:
RegEx.test vs Array.includes
RegEx.test vs Array.includes (Lowercase)
RegEx.match vs Array.includes
RegEx.test vs Array.includes -- 3 options
Comments
Confirm delete:
Do you really want to delete benchmark?