Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs loop for word count
(version: 0)
Comparing performance of:
for loop vs regex
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
for loop
function randomNumber(min, max) { const range = max - min; const randomNumber = Math.random() * range; return randomNumber + min; } function word(length) { let result = ""; const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const charactersLength = characters.length; let counter = 0; while (counter < length) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); counter += 1; } return result; } const sentences = []; let targetWords = 10_000; while (targetWords > 0) { const numWords = randomNumber(1, 20); const words = []; for (let i = 0; i < numWords; i++) { words.push(word(randomNumber(3, 7))); } sentences.push(words.join(Math.random() > 0.5 ? "\n" : " ")); targetWords -= numWords } function isWhitespace(c) { return c === " " || c === "\n"; } function countWords(s) { let count = 0; let onWhitespace = true; for (const c of s) { if (onWhitespace) { if (!isWhitespace(c)) { onWhitespace = false; count += 1; } } else if (isWhitespace(c)) { onWhitespace = true; } } return count; } sentences.map(countWords)
regex
function randomNumber(min, max) { const range = max - min; const randomNumber = Math.random() * range; return randomNumber + min; } function word(length) { let result = ""; const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const charactersLength = characters.length; let counter = 0; while (counter < length) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); counter += 1; } return result; } const sentences = []; let targetWords = 10_000; while (targetWords > 0) { const numWords = randomNumber(1, 20); const words = []; for (let i = 0; i < numWords; i++) { words.push(word(randomNumber(3, 7))); } sentences.push(words.join(Math.random() > 0.5 ? "\n" : " ")); targetWords -= numWords } const reg = new RegExp(/\S+/g) function countWords(string) { const words = string.match(reg); return words ? words.length : 0; } sentences.map(countWords)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different approaches for word counting in JavaScript can be fascinating. **Benchmark Overview** The benchmark consists of two test cases: "for loop" and "regex". Both test cases generate a large number of sentences with random words and then count the number of words in each sentence using different approaches. **For Loop Approach** In the "for loop" approach, a while loop is used to iterate over the generated sentences. For each sentence, a counter variable is initialized to 0, and then iterates over each character in the sentence. If the character is not a whitespace, the counter is incremented. This approach uses a simple iteration technique. **Pros:** 1. Easy to understand and implement. 2. Does not require any additional libraries or dependencies. 3. Can be optimized for specific use cases (e.g., using caching). **Cons:** 1. May be slower than other approaches due to the overhead of the while loop and character iteration. 2. Requires manual management of the counter variable. **Regex Approach** In the "regex" approach, a regular expression is used to match all non-whitespace characters in the sentence. The `match()` method returns an array of matches, and the length of this array is returned as the word count. **Pros:** 1. Can be faster than the for loop approach due to the use of optimized regex engines. 2. Does not require manual iteration over characters. **Cons:** 1. Requires a good understanding of regular expressions and their syntax. 2. May have performance issues if the regex is too complex or has many unnecessary anchors. **Library Used:** In both test cases, no specific libraries are used beyond the built-in JavaScript functions (e.g., `Math.random()`). However, it's worth noting that the use of regular expressions involves a small overhead due to the creation and compilation of the regex pattern. **Special JS Features or Syntax:** None of the approaches rely on any special JavaScript features or syntax beyond basic iteration techniques and function definitions.
Related benchmarks:
Counting words space - match vs split
Count words. regexp vs split
test regex vs loop
Regex OR vs. Multiple Regex Expressions 4
slicing regex vs sticky regex
Comments
Confirm delete:
Do you really want to delete benchmark?