HTML Preparation code:
x
 
1
<script>
2
3
var _arr = (() => {
4
  const produce = (n) => new Array(n + 1).fill().map(() => `${Math.random()}`.slice(2)).join(']');
5
  const a0 = new Array(20).fill().map(() => produce(0));
6
  const a1 = new Array(20).fill().map(() => produce(1));
7
  const a2 = new Array(20).fill().map(() => produce(2));
8
  const a3 = new Array(20).fill().map(() => produce(3));
9
  const a4 = new Array(20).fill().map(() => produce(4));
10
  const arr = [].concat(a0).concat(a1).concat(a2).concat(a3).concat(a4);
11
  arr.sort(() => Math.random() - 0.5);
12
  return arr;
13
})();
14
15
function getPositions(path) {
16
  const positions = [];
17
  for (let i = 0; i < path.length; i++) {
18
    if (path[i] === ']') {
19
      positions.push(i);
20
    }
21
  }
22
  return positions;
23
}
24
                                  
25
                                  
26
function getPositionsIndexOf(path) {
27
  const positions = [];
28
  for (let i = 0; (i = path.indexOf(']', i)) >= 0; i++) {
29
    positions.push(i);
30
  }
31
  return positions;
32
}
33
34
</script>
Tests:
  • reg.exec

     
    const arr = _arr;
    const len = arr.length;
    let r = 0;
    for (let i = 0; i < len; i++) {
      const reg = /\]/g;
      const positions = [];
      let match;
      const basic_path = arr[i];
      while ((match = reg.exec(basic_path)) !== null) {
        positions.push(match.index);
      }
      r += positions.length;
    }
    window.tmp_r1 = r;
  • for-loop

     
    const arr = _arr;
    const len = arr.length;
    let r = 0;
    for (let i = 0; i < len; i++) {
      const basic_path = arr[i];
      const positions = getPositions(basic_path);
      r += positions.length;
    }
    window.tmp_r2 = r;
  • for-indexOf

     
    const arr = _arr;
    const len = arr.length;
    let r = 0;
    for (let i = 0; i < len; i++) {
      const basic_path = arr[i];
      const positions = getPositionsIndexOf(basic_path);
      r += positions.length;
    }
    window.tmp_r3 = r;
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    reg.exec
    for-loop
    for-indexOf

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
reg.exec 140010.5 Ops/sec
for-loop 50403.7 Ops/sec
for-indexOf 105460.1 Ops/sec