Script Preparation code:
AخA
 
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

    x
     
    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>

Previous results

Experimental features:

  • Test case name Result
    regex
    manual
    switch
    inline

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36
Chrome 112 on Linux
View result in a separate tab
Test name Executions per second
regex 105483.6 Ops/sec
manual 123752.8 Ops/sec
switch 138736.7 Ops/sec
inline 137117.0 Ops/sec