Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Chrome 102
Windows
Desktop
2 years ago
Test name Executions per second
RegEx 2541434.5 Ops/sec
For Loop 273720.7 Ops/sec
HTML Preparation code:
AخA
 
1
var string = "passw)odas4gsdfsdf";
Script Preparation code:
 
var string = "aaaaabbbbbccccddd";
Tests:
  • RegEx

     
    var s = string.match(/([a-zA-Z])\1*/g)||[];
    return s.map(function(itm){
      return [itm.charAt(0), itm.length];
    });
  • For Loop

     
    var result = [[string[0], 1]]
    for(let i = 1; i < string.length; i++) {
      const char = string[i]
      const lastChar = result.at(-1)
      
      if(char === lastChar[0]){
        lastChar[1] = lastChar[1] + 1
      } else {
        result.push([char, 1])
      }
    }