Script Preparation code:
AخA
 
var value = '78005553535';
var regex = /(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})/;
var mask = '+$1 ($2) $3-$4-$5';
Tests:
  • Slice

    x
     
    const countryCode = value.slice(0, 1);
    const regionCode = value.slice(1, 4);
    const partOne = value.slice(4, 7);
    const partTwo = value.slice(7, 9);
    const partThree = value.slice(9, 11);
    strOut = `+${countryCode} (${regionCode}) ${partOne}-${partTwo}-${partThree}`;
  • Replace compile

     
    strOut = value.replace(regex, mask);
  • Replace inline

     
    strOut = value.replace(/(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})/, mask);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Slice
    Replace compile
    Replace inline

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36
Chrome 100 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
Slice 1671550.9 Ops/sec
Replace compile 980714.1 Ops/sec
Replace inline 1035687.8 Ops/sec