Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
match vs for loop
(version: 0)
Comparing performance of:
for loop vs match counter
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testString = "98n1s fjodn fsdnfjsdnfdsj sffJNFAJDNFIJNDFJDd fbdh fkbDKHF23" function loopCounter(str) { let counter = 0 for (const char of str) if (char.toLowerCase() !== char.toUpperCase() && char.toLowerCase() === char) counter++ return counter } function matchCounter(str) { return (str.match(/[a-z]/g) || []).length }
Tests:
for loop
loopCounter(testString)
match counter
matchCounter(testString)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
match counter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
574147.7 Ops/sec
match counter
1394333.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Description** The benchmark compares two approaches to counting the number of duplicate characters in a string: 1. **For Loop**: The `loopCounter` function uses a traditional for loop to iterate over each character in the input string `testString`. It increments a counter variable whenever it encounters a character that is both lowercase and uppercase (i.e., equal to its own case). 2. **Regex (match)**: The `matchCounter` function uses a regular expression to match all characters in the input string that are letters (either uppercase or lowercase). It then returns the length of the resulting array, which represents the number of duplicate characters. **Options Compared** The benchmark compares the performance of these two approaches: * **For Loop**: A traditional, explicit loop-based approach. * **Regex (match)**: A regular expression-based approach using the `match` method to extract matching patterns from the input string. **Pros and Cons** * **For Loop**: + Pros: - Easy to understand and implement for developers familiar with loops. - Can be optimized for specific use cases. + Cons: - May have performance issues due to the explicit loop structure. - Requires manual management of loop variables and logic. * **Regex (match)**: + Pros: - Concise and expressive syntax. - Fast and efficient when dealing with regular expressions. + Cons: - Can be slower than a traditional for loop due to the overhead of regular expression compilation and matching. - May have performance issues if used with large input strings. **Library Usage** The benchmark uses the `match` method, which is a built-in JavaScript method that returns an array of matches or null if no matches are found. This method is part of the JavaScript standard library, making it a widely available and platform-independent solution. **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The code uses standard JavaScript constructs like loops, conditional statements, and functions. **Alternative Approaches** Other alternative approaches to counting duplicate characters could include: * Using a `Set` data structure to keep track of unique characters. * Utilizing the ` Intl.Collator` API to compare strings based on locale-specific collation rules. * Implementing a custom hash table or array-based solution for efficient character counting. These alternatives might offer better performance, readability, or maintainability in specific scenarios, but they are not as straightforward to implement as the for loop and regex approaches.
Related benchmarks:
Count string occurrence
Count char occurrence in string
Alphanumeric String test 1
Alphanumeric String test 2
regex vs js - not Alphanumeric String
Comments
Confirm delete:
Do you really want to delete benchmark?