Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regexp test for finding invalid character
(version: 0)
Comparing performance of:
Includes vs Regexp
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string }
Tests:
Includes
const headerName = '23434łdfwfwfweffwefwefe'; const HTTP_HEADER_NAME_CHARS = '!#$%&*+-.0123456789ABCDEFGHIJKLMNOPQRSTUWVXYZ^_`abcdefghijklmnopqrstuvwxyz|~'; for (const character of headerName) { if (!HTTP_HEADER_NAME_CHARS.includes(character)) { return character; } } return null;
Regexp
const headerName = '23434łdfwfwfweffwefwefe'; const HTTP_HEADER_NAME_CHARS = '!#$%&*+-.0123456789ABCDEFGHIJKLMNOPQRSTUWVXYZ^_`abcdefghijklmnopqrstuvwxyz|~'; const reg = new RegExp('^[' + escapeRegExp(HTTP_HEADER_NAME_CHARS) + ']$'); reg.test(headerName);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
Regexp
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
20406376.0 Ops/sec
Regexp
1727072.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to measure the performance of two approaches: using regular expressions (regex) and manual string checking. The goal is to find an invalid character in a given string. **Options Compared** There are two main options compared: 1. **Regex**: Using a built-in JavaScript regex engine to check if a character is valid. 2. **Manual String Checking**: Manually iterating through each character of the input string and checking its validity using a predefined set of allowed characters. **Pros and Cons** **Regex:** Pros: * More concise and expressive code * Can handle complex patterns and multiple matching criteria * Built-in engine takes care of optimization and caching Cons: * May be slower than manual string checking for large inputs due to overhead from the regex engine * Can be less readable or maintainable for simple use cases **Manual String Checking:** Pros: * Faster execution time for large inputs due to reduced overhead * Can be more readable and maintainable for simple use cases Cons: * More verbose code, which can lead to errors if not implemented carefully * Requires explicit maintenance of the allowed characters set **Library Used (if applicable)** In this benchmark, the `escapeRegExp` library is used. This library takes a string as input and returns an escaped version of it, useful for regex patterns. **Special JS Feature or Syntax** None are explicitly mentioned in the provided code snippets. **Other Alternatives** If you want to implement your own string validation solution without using regex, you could consider: 1. **Regular expression alternatives**: Look into other regex flavors like PCRE (Perl-Compatible Regular Expressions) or LuaJIT's regex engine. 2. **Custom string checking functions**: Implement a custom function that iterates through each character of the input string and checks its validity manually. Keep in mind that these alternatives might not offer significant performance improvements over the built-in JavaScript regex engine and should be carefully evaluated for use cases and complexity. In conclusion, the benchmark provides a straightforward comparison between using regex and manual string checking. By understanding the pros and cons of each approach, developers can make informed decisions about which solution to choose depending on their specific requirements and performance constraints.
Related benchmarks:
Wildcard Match Test
string replaceAll test
new RegExp vs literal with groups
Find tokens in string with regexp vs substring
Comments
Confirm delete:
Do you really want to delete benchmark?