Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.prototype.split vs String.prototype.match for word count
(version: 0)
Test e.g. counting words by using String.prototype.match versus String.prototype.split with a regular expression.
Comparing performance of:
String.prototype.split vs String.prototype.match
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
baseString = "Quisque sit amet maximus felis, ut pulvinar lacus. Suspendisse mollis velit metus, id venenatis purus sollicitudin sed. Ut sagittis, neque at suscipit bibendum, velit nibh gravida purus, at eleifend nibh diam a sapien. Nam suscipit tristique enim non dignissim. Aliquam accumsan vehicula quam, vel porta arcu pretium sit amet. Curabitur venenatis est sapien, eu ullamcorper urna vestibulum at. In et rhoncus felis. Etiam dictum, lacus nec suscipit rhoncus, massa odio tempor orci, in feugiat libero urna vel ex. Sed magna neque, pretium auctor rutrum quis, auctor sit amet lectus. Proin sit amet pharetra lacus. Quisque et risus odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Duis dapibus sodales risus quis ultricies."; matchRegex = /[^\s]+/g; splitRegex = /\s+/g;
Tests:
String.prototype.split
let splitCount = baseString.split(splitRegex);
String.prototype.match
let matchCount = baseString.match(matchRegex).length;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String.prototype.split
String.prototype.match
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; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.prototype.split
341707.3 Ops/sec
String.prototype.match
364119.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **What is being tested?** The benchmark tests two approaches to count words in a string: 1. `String.prototype.split`: This method splits the input string into an array of substrings using a regular expression. In this case, the regex `/\\s+/g` matches one or more whitespace characters (`\\s+`) and the `g` flag at the end makes it match all occurrences in the string. 2. `String.prototype.match`: This method searches for a pattern in a string and returns an array of matches. In this case, the regex `/[^\\s]+/g` matches one or more characters that are not whitespace (`[^\\s]+`) and the `g` flag makes it match all occurrences. **Options compared** The benchmark compares two options: * Using `String.prototype.split` with a regular expression to split the string into words. * Using `String.prototype.match` with a regular expression to count the number of non-whitespace characters in the string, effectively giving the word count. **Pros and cons** Here's a brief analysis of each approach: * **`String.prototype.split`**: + Pros: Simple and easy to understand. It directly splits the string into words using whitespace as the delimiter. + Cons: Can be slow for very large strings or strings with many consecutive whitespace characters, as it creates an array of substrings. * **`String.prototype.match`**: + Pros: Efficient for counting non-whitespace characters, as it only scans the string once. It also returns a count that can be easily converted to a word count. + Cons: Requires more regex expertise and might not work as expected if the input string has unusual whitespace characters. **Library** None of these approaches rely on a specific library beyond what's built into JavaScript (i.e., the `String` object). However, it's worth noting that modern browsers have optimized implementations of `String.prototype.split` and `String.prototype.match`, which can make a difference in performance. **Special JS feature or syntax** There isn't any special JavaScript feature or syntax being tested here. The benchmarks use standard JavaScript features like regular expressions and string methods. **Other alternatives** If you wanted to write your own word counting algorithm, you might consider using: * `String.indexOf` with multiple calls to increment a counter * A loop that manually checks each character in the string * Using a more advanced regex approach, such as matching word boundaries or using lookaheads/lookbehinds However, these alternatives would likely be slower and less efficient than using built-in methods like `String.prototype.split` and `String.prototype.match`. Overall, this benchmark provides a useful comparison of two common approaches to counting words in JavaScript strings.
Related benchmarks:
str.match vs str.Split
String.Split and String.Match
str.match vs str.split vs deconstruction
split vs memoize
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?