Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[find char positions] reg.exec vs for-loop vs for-indexOf
(version: 0)
Comparing performance of:
reg.exec vs for-loop vs for-indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reg.exec
for-loop
for-indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
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/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reg.exec
140010.5 Ops/sec
for-loop
50403.7 Ops/sec
for-indexOf
105460.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, testing different approaches for finding character positions in an array of strings. **Benchmark Definition JSON** The provided benchmark definition JSON represents three test cases: 1. `reg.exec`: Tests the usage of the `exec()` method with a regular expression to find character positions in an array of strings. 2. `for-loop`: Tests a traditional `for` loop approach to find character positions in an array of strings. 3. `for-indexOf`: Tests the usage of the `indexOf()` method to find character positions in an array of strings. **Options Compared** The three test cases compare different approaches for finding character positions: * **reg.exec**: Uses a regular expression to search for character positions (`]`) in each string. * **for-loop**: Iterates through the array using a traditional `for` loop, calling `getPositions()` or `getPositionsIndexOf()` function on each element. * **for-indexOf**: Uses the `indexOf()` method to find the index of the first occurrence of `]` in each string. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **reg.exec**: + Pros: Efficient, lightweight, and easy to use. + Cons: May not work well with non-ASCII characters or complex regex patterns. * **for-loop**: + Pros: Easy to understand, flexible, and can handle complex logic. + Cons: May be slower than `reg.exec` for large datasets. * **for-indexOf**: + Pros: Fast, efficient, and works well with large datasets. + Cons: Can be slower than `reg.exec` for small datasets. **Library Usage** The benchmark definition JSON uses two custom functions: * `getPositions(path)`: Returns an array of indices where `]` appears in the input string `path`. * `getPositionsIndexOf(basic_path)`: Similar to `getPositions()`, but uses the `indexOf()` method instead. These functions are likely implemented by the user, as they don't appear to be part of a standard JavaScript library. **Special JS Features or Syntax** There are no special JS features or syntax mentioned in this benchmark definition. The focus is on comparing different approaches for finding character positions. **Alternatives** Other alternatives for finding character positions could include: * Using a library like `lodash` with its `indexOf()` and `findIndex()` methods. * Implementing a custom iterator or generator function to iterate through the array. * Using a different approach, such as using a trie data structure to store the strings.
Related benchmarks:
Array.concat vs Array.prototype.concat.apply
Array.prototype.concat vs Array.prototype.splice
Array Splice vs Concat
[find char positions] reg.exec vs for-indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?