Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs regex in vowel counting
(version: 0)
Comparing performance of:
forEach version vs regexVersion
Created:
9 years ago
by:
Guest
Jump to the latest result
Tests:
forEach version
// useful for HtmlCollection, NodeList, String types function forEach(array, callback, scope){for (var i=0,n=array.length; i<n; i++)callback.call(scope, array[i], i, array);} // passes back stuff we need function countVowels(input) { var vowels = 'aeiou', result = 0; forEach( input, function(char){ if (vowels.indexOf(char) != -1) result++; } ); return result; } countVowels("goes");
regexVersion
function VowelCount(str) { var pattern = /[aeiou]/i; var vowelCount = 0; for (var i = 0; i < str.length; i++) { if (pattern.test(str[i])){ vowelCount++; } } return vowelCount; } VowelCount("goes")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach version
regexVersion
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach version
14167874.0 Ops/sec
regexVersion
18621818.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark compares two approaches to count the number of vowels in a given string: using the `forEach` method and using a regular expression (`regex`). We'll break down what each approach does, its pros and cons, and discuss other considerations. **1. Using `forEach`:** ```javascript function forEach(array, callback, scope) { for (var i = 0, n = array.length; i < n; i++) { callback.call(scope, array[i], i, array); } } function countVowels(input) { var vowels = 'aeiou', result = 0; forEach(input, function(char) { if (vowels.indexOf(char) != -1) result++; }); return result; } ``` The `forEach` method is a built-in JavaScript function that applies a given callback function to each element of an array. In this case, the callback function checks if the character is a vowel and increments the result accordingly. **Pros:** * Easy to read and understand. * Works with any iterable (array, string, etc.). **Cons:** * May have performance overhead due to the callback function invocation. * Can be slower for very large strings or arrays. **2. Using regular expression (`regex`):** ```javascript function VowelCount(str) { var pattern = /[aeiou]/i; var vowelCount = 0; for (var i = 0; i < str.length; i++) { if (pattern.test(str[i])) { vowelCount++; } } return vowelCount; } ``` The regular expression `[aeiou]` matches any character that is a vowel (both lowercase and uppercase). The `i` flag at the end makes the match case-insensitive. In this implementation, we use the `test()` method to check if each character is a vowel. **Pros:** * Fast and efficient for matching patterns. * Can be more concise than using `forEach`. **Cons:** * May not work as expected with very large strings or arrays due to memory constraints. * Less readable for some developers (due to the use of regex). **Library usage:** In this benchmark, the `forEach` method is a built-in JavaScript function. No external library is used. **Special JS feature:** None mentioned in this specific benchmark. However, it's worth noting that JavaScript has several features that can affect performance, such as: * `let` and `const` declarations (vs. traditional `var`) * Arrow functions * Spread operators * Object destructuring These features can impact the performance of certain code snippets, but their effects are often subtle and depend on the specific use case. **Alternatives:** If you're interested in exploring alternative approaches to this benchmark, here are a few options: 1. **Lodash**: A popular JavaScript utility library that provides various functions for tasks like array manipulation. 2. **Radium**: Another JavaScript library that offers performance-oriented string matching and searching algorithms. 3. **V8**: The open-source JavaScript engine used in Google Chrome, which can be used as a reference implementation for benchmarking JavaScript performance. Keep in mind that the choice of alternative approach depends on your specific use case and requirements. I hope this explanation helps!
Related benchmarks:
for (i < n) vs forEach vs for...of
.forEach vs for const of
RegEx.exec vs StrRaasdhakshjding.match
Regex tests Dani
DTMF: array includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?