var string = "isAlphaNumeric091";
var string = "isAlphaNumeric091";
/^[a-z0-9]+$/i.test(string)
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)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex | |
charCodeAt |
Test name | Executions per second |
---|---|
Regex | 21662038.0 Ops/sec |
charCodeAt | 29857242.0 Ops/sec |
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:
/^[a-z0-9]+$/i.test(string)
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)
isAlphaNumeric
that checks each character in the input string using charCodeAt
. If any non-alphanumeric character is found, the function returns false
.Both test cases aim to measure the performance of the browser when running these specific tests.
Options Compared
The two test cases compare:
charCodeAt
implementationPros and Cons
Other Considerations
charCodeAt
implementation is self-contained.charCodeAt
implementation demonstrates the use of a specific JavaScript feature: iterating over a string and checking character codes.Alternatives
If you were to rewrite this benchmark with alternative approaches:
regexjs
) for more control and potential better performance.Keep in mind that these alternatives may introduce additional complexity, overhead, or platform dependencies, which should be carefully considered when choosing an approach.