Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes with regex vs startWith
(version: 0)
Comparing performance of:
startsWith vs match vs test
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
startsWith
"@MyVariable".startsWith("@")
match
"@MyVariable".match(/@.*/)
test
/@*./.test("@MyVariable")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
startsWith
match
test
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/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
2085696256.0 Ops/sec
match
38584548.0 Ops/sec
test
72152584.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an explanation of the benchmark, its options, pros and cons, and other considerations. **Benchmark Overview** The benchmark measures the performance of three different approaches to test if a variable `@MyVariable` starts with the string `"@"`. The three approaches are: 1. `startsWith` 2. `match` 3. `/regex/` These tests aim to evaluate the speed of each approach in checking if a string starts with a specific prefix. **Library and Purpose** In this benchmark, the library used is none, but rather JavaScript's built-in functions for regular expressions (regex) and string manipulation. The purpose of these approaches is to test their performance when checking if a string starts with a certain prefix. **Approach 1: `startsWith`** `startsWith` is a built-in function in JavaScript that checks if a string starts with another string. It returns `true` if the first character of the original string matches the first character of the specified string, and `false` otherwise. Pros: * Simple and intuitive to use * Fast execution time Cons: * May not be suitable for non-ASCII strings (e.g., accented characters) * Can return false positives if the starting character is not a match (e.g., "abc@" would return true) **Approach 2: `match`** `match` is another built-in function in JavaScript that returns an array containing all matches of the specified regex pattern within the string. In this case, we're using `/^@/.test()` to test if the string starts with the prefix `"@"`. Pros: * Can handle non-ASCII strings * Returns a clear result (either `true` or `false`) Cons: * May be slower than `startsWith` due to the regex parsing and evaluation * Can return false positives if the regex pattern is not specific enough **Approach 3: `/regex/`** This approach uses a regex pattern `/@*./` to test if the string starts with the prefix `"@"`. The `.*` in the regex pattern matches any characters (except newline) zero or more times. Pros: * Can handle non-ASCII strings * Provides fine-grained control over the matching process Cons: * May be slower than `startsWith` due to the regex parsing and evaluation * Can return false positives if the prefix is not matched exactly (e.g., "abc@" would match) **Other Considerations** In addition to performance, another consideration is memory usage. The `match` approach may use more memory since it needs to parse the entire regex pattern. **Alternatives** If you want to test similar benchmarks using alternative approaches, here are some options: * Use a regular expression library like Uregex or RegExJS for more advanced regex capabilities. * Implement your own custom function to check if a string starts with a prefix (e.g., `myStartsWith`). * Test the performance of other built-in functions in JavaScript, such as `includes`, `localeCompare`, or `indexOf`. Keep in mind that the specific approach and implementation may vary depending on the requirements of your project. In summary, this benchmark aims to evaluate the performance of three different approaches to test if a variable starts with a certain prefix. The `startsWith` function is likely the fastest, followed by the regex-based approaches.
Related benchmarks:
Case insensitive RegEx.test vs. String.includes when string doesn’t match
Case Insensitive RegEx.test vs. String.includes
startsWith vs includes vs regex test when no match
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?