Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Alphanumeric String
(version: 0)
Check if a string only contains alpha-numeric characters.
Comparing performance of:
Regex vs charCodeAt
Created:
4 years ago
by:
Registered User
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
55639524.0 Ops/sec
charCodeAt
30533926.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark definition represents a simple test that checks if a given string only contains alpha-numeric characters (letters and numbers). The script preparation code creates a sample string "isAlphaNumeric091" for testing purposes. **Test Cases** There are two individual test cases: 1. **Regex Test Case** * Benchmark Definition: `/^[a-z0-9]+$/i.test(string)` * This test case uses a regular expression (regex) to check if the input string only contains alphanumeric characters. 2. **charCodeAt Test Case** * Benchmark Definition: `function isAlphaNumeric(str) {\r\n for (let i = 0; i < str.length; i++) {\r\n let code = str.charCodeAt(i)\r\n if (!(code > 47 && code < 58) && // numeric (0-9)\r\n !(code > 64 && code < 91) && // upper alpha (A-Z)\r\n !(code > 96 && code < 123)) { // lower alpha (a-z)\r\n return false\r\n }\r\n }\r\n return true\r\n}\r\n\r\nisAlphaNumeric(string)` * This test case uses a custom JavaScript function `isAlphaNumeric` that checks each character in the input string using `charCodeAt`. If any non-alphanumeric character is found, the function returns `false`. 3. **Purpose** Both test cases aim to measure the performance of the browser when running these specific tests. **Options Compared** The two test cases compare: * Regex vs Custom `charCodeAt` implementation * Performance difference between using a built-in regex engine and a custom implementation **Pros and Cons** 1. **Regex Test Case** * Pros: + Widely supported by most browsers + Concise and efficient code * Cons: + May be slower due to the overhead of the regex engine + Limited control over the matching process 2. **charCodeAt Test Case** * Pros: + More control over the matching process + Potential for better performance, as it avoids regex engine overhead * Cons: + Requires manual implementation and maintenance + May be less concise than a regex solution **Other Considerations** * **Library usage**: Neither test case uses an external library. The regex test case relies on the browser's built-in regex engine, while the custom `charCodeAt` implementation is self-contained. * **JS feature or syntax**: The custom `charCodeAt` implementation demonstrates the use of a specific JavaScript feature: iterating over a string and checking character codes. * **Browser variations**: It's essential to note that these tests are likely designed to run on different browsers, which may have varying levels of support for regex engines or performance characteristics. **Alternatives** If you were to rewrite this benchmark with alternative approaches: 1. Use a regular expression engine from a third-party library (e.g., `regexjs`) for more control and potential better performance. 2. Implement a custom string matching algorithm using bitwise operations, character flags, or other low-level techniques. 3. Use a different testing framework or tool that provides built-in support for regex or custom string matching. Keep in mind that these alternatives may introduce additional complexity, overhead, or platform dependencies, which should be carefully considered when choosing an approach.
Related benchmarks:
Alphanumeric
alpha-numeric string
Alphanumeric String test 1
regex vs js - not Alphanumeric String
Comments
Confirm delete:
Do you really want to delete benchmark?