Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs js - not Alphanumeric String
(version: 0)
Check if a string only contains alpha-numeric characters.
Comparing performance of:
Regex vs charCodeAt
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
var string = "isAlphaNumeric091,";
Script Preparation code:
var string = "isAlphaNumeric091,";
Tests:
Regex
/^[a-z0-9]+$/i.test(string)
charCodeAt
function isAlphaNumeric(str) { for (let i = 0; i < str.length; i++) { let code = str.charCodeAt(i) if (!(code > 47 && code < 58) && // numeric (0-9) !(code > 64 && code < 91) && // upper alpha (A-Z) !(code > 96 && code < 123)) { // lower alpha (a-z) return false } } return true } isAlphaNumeric(string)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
charCodeAt
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
31974914.0 Ops/sec
charCodeAt
30141672.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition specifies two test cases: 1. `regex`: This test case uses a regular expression to check if a string contains only alphanumeric characters (letters and numbers). The regular expression `/^[a-z0-9]+$/i.test(string)` checks the input string `string` for matches against this pattern. 2. `charCodeAt`: This test case manually checks each character in the input string using the `charCodeAt()` method to get the ASCII code of each character. It then checks if the code falls within the range of alphanumeric characters (47-57 for numbers, 64-90 for uppercase letters, and 96-122 for lowercase letters). If any character does not meet this condition, it returns `false`. Otherwise, it returns `true`. **Comparison Options** The two test cases are compared to measure which approach is faster: * **Regex**: Uses a regular expression engine to check the input string. This approach can be more readable and easier to maintain than manual ASCII code checks. * **charCodeAt**: Manually checks each character in the input string using the `charCodeAt()` method. **Pros and Cons** Here are some pros and cons of each approach: **Regex** Pros: * Easier to read and maintain * Can handle more complex patterns (e.g., non-alphanumeric characters) * Often faster than manual ASCII code checks Cons: * May be slower for very long input strings due to the overhead of regular expression parsing * May have performance issues with certain character sets or Unicode combinations **charCodeAt** Pros: * Typically faster than regex for short to medium-length input strings * More predictable and controlled, as it only checks ASCII codes Cons: * More difficult to read and maintain, especially for longer input strings * Not suitable for handling non-ASCII characters or complex patterns **Library Used** The `charCodeAt()` test case uses the built-in JavaScript method `charCodeAt()`, which is a part of the ECMAScript standard. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark, other than regular expressions. The use of `i` at the end of the regex pattern `/^[a-z0-9]+$/i` makes it case-insensitive. **Other Alternatives** If you wanted to test alternative approaches, some possible options could be: * Using a different programming language (e.g., C++, Python) for manual ASCII code checks * Using a more complex regular expression engine (e.g., `grep`, `sed`) for regex checks * Testing other string validation algorithms (e.g., Unicode code point validation) * Comparing the performance of regex engines from different vendors (e.g., JavaScript, Java, .NET)
Related benchmarks:
Alphanumeric
Alphanumeric String
Alphanumeric String test 1
Alphanumeric String test 2
Comments
Confirm delete:
Do you really want to delete benchmark?