Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex whitespace vs manual checking
(version: 2)
Comparing performance of:
regex vs manual vs switch vs inline
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var string = `A CBASCDASC cascasc qwcqwcqwc qwcqwfcqwc asas as as asas`
Tests:
regex
let i = 0; let num = 0; while (i < string.length) { if (/\s/.test(string[i])) { ++num; } i++; }
manual
let i = 0; let num = 0; while (i < string.length) { if (" \f\n\r\t\v\u00A0\u2028\u2029".includes(string[i])) { ++num; } i++; }
switch
let i = 0; let num = 0; function test(ch) { switch (ch) { case " ": case "\f": case "\n": case "\r": case "\t": case "\v": case "\u00A0": case "\u2028": case "\u2029": return true; } return false; } while (i < string.length) { const text = string[i]; if (test(text)) { ++num; } i++; }
inline
let i = 0; let num = 0; while (i < string.length) { const ch = string[i]; switch (ch) { case " ": case "\f": case "\n": case "\r": case "\t": case "\v": case "\u00A0": case "\u2028": case "\u2029": ++num; } i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex
manual
switch
inline
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
745940.6 Ops/sec
manual
796486.4 Ops/sec
switch
3772638.2 Ops/sec
inline
3361785.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the benchmark being tested on MeasureThat.net. **Benchmark Overview** The benchmark measures the performance of three different approaches for detecting whitespace characters in a string: 1. **Regular Expressions (Regex)** 2. **Manual Checking** using an array of predefined whitespace characters 3. **Switch Statement** **Options Compared** * The options being compared are: + Regular Expressions (Regex) vs Manual Checking **Pros and Cons of Each Approach** * **Regular Expressions (Regex)**: + Pros: concise, flexible, can handle complex patterns + Cons: slower due to the overhead of compiling a regular expression, may have performance issues with very large inputs * **Manual Checking**: + Pros: simple, fast, easy to understand and implement + Cons: requires manually defining all possible whitespace characters, not flexible **Switch Statement** * The Switch statement approach is an optimization over the Manual Checking approach. * It uses a switch statement to check if the current character matches any of the predefined whitespace characters. This can be faster than manual checking because it avoids the need to repeatedly check each character. **Library Used (Manual Checking)** The Manual Checking approach uses an array of strings representing all possible whitespace characters. The library used is not explicitly mentioned, but it's likely a simple array of character literals or a string constant in JavaScript. **Special JS Feature/ Syntax (Switch Statement)** The Switch statement approach uses the `switch` keyword and the `case` clause to define the behavior for each possible input value. **Other Alternatives** If you need to detect whitespace characters, other alternatives might include: * Using a library like `DOMParser` or `XMLSerializer` to parse the string and extract whitespace characters * Implementing a custom parser using JavaScript's built-in string methods (e.g., `indexOf`, `includes`) * Using a third-party library or module that provides whitespace detection functionality Keep in mind that these alternatives may have different trade-offs in terms of performance, simplicity, and flexibility.
Related benchmarks:
Regex detecting whitespace vs trim
Regex detecting whitespace vs trim
Regex testing for whitespace vs trim
Regex removing single whitespace vs multiple whitespaces
Detecting an Empty or Whitespace String using RegEx vs trim
Comments
Confirm delete:
Do you really want to delete benchmark?