Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
[find char positions] reg.exec vs for-loop vs for-indexOf
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
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
Browser:
Chrome 124
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
reg.exec
140010.5 Ops/sec
for-loop
50403.7 Ops/sec
for-indexOf
105460.1 Ops/sec
HTML Preparation code:
<script> var _arr = (() => { const produce = (n) => new Array(n + 1).fill().map(() => `${Math.random()}`.slice(2)).join(']'); const a0 = new Array(20).fill().map(() => produce(0)); const a1 = new Array(20).fill().map(() => produce(1)); const a2 = new Array(20).fill().map(() => produce(2)); const a3 = new Array(20).fill().map(() => produce(3)); const a4 = new Array(20).fill().map(() => produce(4)); const arr = [].concat(a0).concat(a1).concat(a2).concat(a3).concat(a4); arr.sort(() => Math.random() - 0.5); return arr; })(); function getPositions(path) { const positions = []; for (let i = 0; i < path.length; i++) { if (path[i] === ']') { positions.push(i); } } return positions; } function getPositionsIndexOf(path) { const positions = []; for (let i = 0; (i = path.indexOf(']', i)) >= 0; i++) { positions.push(i); } return positions; } </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;