Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[find char positions] reg.exec vs for-indexOf
(version: 0)
Comparing performance of:
reg.exec 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; })(); </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-indexOf
const arr = _arr; const len = arr.length; let r = 0; for (let i = 0; i < len; i++) { const positions = []; const basic_path = arr[i]; for (let j = 0; (j = basic_path.indexOf(']', j)) >= 0; j++) { positions.push(j); } r += positions.length; } window.tmp_r3 = r;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reg.exec
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
155138.6 Ops/sec
for-indexOf
322478.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark compares two approaches for finding all occurrences of a specific character (`']`) in an array: `reg.exec` vs `for-indexOf`. The test case uses a randomly generated array of 20 elements, each containing a string slice of a random length starting from index 2. This creates a scenario where the pattern to find is scattered throughout the array. **Library and Syntax** * **reg**: `reg.exec` is a part of the `RegExp` object in JavaScript, which provides regular expression matching operations. + Purpose: To match a specified pattern (in this case, `\\]`) within a string. + Usage: The `exec()` method returns an array containing the matched elements. **Test Cases** The benchmark consists of two test cases: 1. **reg.exec** 2. **for-indexOf** Both approaches aim to find all occurrences of the closing bracket (`'']`) in each element of the array and sum up their indices. **Options Compared** * **reg.exec**: Uses a regular expression to search for the pattern within each string element. + Pros: - Efficient for searching patterns with wildcards or anchors. - Can handle large datasets by leveraging the optimized RegExp engine. + Cons: - May have poor performance when dealing with very long strings or complex patterns, as it incurs a compilation overhead. - Limited control over optimization and caching mechanisms. * **for-indexOf**: Iterates through each string element and uses the `indexOf()` method to find occurrences of the pattern. + Pros: - Simple, straightforward implementation with minimal dependencies. - Can be optimized for small arrays or specific use cases by leveraging the `indexOf()` method's caching mechanism. + Cons: - May have poor performance when dealing with large datasets due to the iteration overhead. **Other Considerations** * **Cacheability**: The `reg.exec` approach is cacheable, meaning that once a regular expression object is created and compiled, subsequent executions can reuse this cached representation. This can lead to significant performance improvements for repeated searches with the same pattern. * **Regular Expression Complexity**: Complex patterns or large strings can negatively impact performance due to the compilation overhead. **Alternatives** Other approaches to finding occurrences of a specific character in an array might include: * Using `String.prototype.indexOf()` and chaining multiple calls to iterate through each element * Employing a custom implementation using bitwise operations (e.g., using `bitset` or `uint32array`) * Leveraging libraries like `lodash` with its `indexOf` utility function
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-loop vs for-indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?