Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex match() array vs matchAll() iterator
(version: 0)
Comparing performance of:
match vs matchAll
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var re = /\w+/g var text = `Miss Brooke had that kind of beauty which seems to be thrown into relief by poor dress. Her hand and wrist were so finely formed that she could wear sleeves not less bare of style than those in which the Blessed Virgin appeared to Italian painters; and her profile as well as her stature and bearing seemed to gain the more dignity from her plain garments, which by the side of provincial fashion gave her the impressiveness of a fine quotation from the Bible,—or from one of our elder poets,—in a paragraph of to-day’s newspaper. She was usually spoken of as being remarkably clever, but with the addition that her sister Celia had more common-sense. Nevertheless, Celia wore scarcely more trimmings; and it was only to close observers that her dress differed from her sister’s, and had a shade of coquetry in its arrangements; for Miss Brooke’s plain dressing was due to mixed conditions, in most of which her sister shared. The pride of being ladies had something to do with it: the Brooke connections, though not exactly aristocratic, were unquestionably “good:” if you inquired backward for a generation or two, you would not find any yard-measuring or parcel-tying forefathers—anything lower than an admiral or a clergyman; and there was even an ancestor discernible as a Puritan gentleman who served under Cromwell, but afterwards conformed, and managed to come out of all political troubles as the proprietor of a respectable family estate. Young women of such birth, living in a quiet country-house, and attending a village church hardly larger than a parlor, naturally regarded frippery as the ambition of a huckster’s daughter. Then there was well-bred economy, which in those days made show in dress the first item to be deducted from, when any margin was required for expenses more distinctive of rank. Such reasons would have been enough to account for plain dress, quite apart from religious feeling; but in Miss Brooke’s case, religion alone would have determined it; and Celia mildly acquiesced in all her sister’s sentiments, only infusing them with that common-sense which is able to accept momentous doctrines without any eccentric agitation. Dorothea knew many passages of Pascal’s Pensees and of Jeremy Taylor by heart; and to her the destinies of mankind, seen by the light of Christianity, made the solicitudes of feminine fashion appear an occupation for Bedlam. She could not reconcile the anxieties of a spiritual life involving eternal consequences, with a keen interest in gimp and artificial protrusions of drapery. Her mind was theoretic, and yearned by its nature after some lofty conception of the world which might frankly include the parish of Tipton and her own rule of conduct there; she was enamoured of intensity and greatness, and rash in embracing whatever seemed to her to have those aspects; likely to seek martyrdom, to make retractations, and then to incur martyrdom after all in a quarter where she had not sought it. Certainly such elements in the character of a marriageable girl tended to interfere with her lot, and hinder it from being decided according to custom, by good looks, vanity, and merely canine affection. With all this, she, the elder of the sisters, was not yet twenty, and they had both been educated, since they were about twelve years old and had lost their parents, on plans at once narrow and promiscuous, first in an English family and afterwards in a Swiss family at Lausanne, their bachelor uncle and guardian trying in this way to remedy the disadvantages of their orphaned condition.`
Tests:
match
(text.match(re) || []).length
matchAll
[...text.matchAll(re)].length
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
match
matchAll
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):
Let's dive into the world of JavaScript benchmarks and explore what's being tested on this particular benchmark. **Benchmark Definition** The benchmark defines two test cases: `match` and `matchAll`. These tests are designed to measure the performance of regular expression matching in JavaScript. **Test Case 1: match** This test case uses a regular expression (`re`) to search for matches in a large text string. The regex pattern is `/\\w+/g`, which matches one or more word characters (letters, numbers, and underscores) globally in the text. The benchmark definition is: `(text.match(re) || []).length` In other words, this test case measures how many times the `match` method can find a match for the regex pattern in the text string. If no match is found, it returns an empty array (`[]`), which has a length of 0. **Test Case 2: matchAll** This test case is similar to the previous one, but instead of using the `match` method, it uses the `matchAll` method to search for all matches of the regex pattern in the text string. The benchmark definition is: `([...text.matchAll(re)].length)` In this case, the `matchAll` method returns an iterator that yields arrays containing all matches of the regex pattern in the text. **Options Compared** The two test cases are comparing the performance of two different approaches to regular expression matching: 1. `match`: This approach uses a single call to the `match` method to search for a single match. 2. `matchAll`: This approach uses an iterator to find all matches of the regex pattern in the text. **Pros and Cons** Here are some pros and cons of each approach: * `match`: + Pros: Simple, efficient, and easy to understand. + Cons: Only finds a single match, which might not be what the user wants if there are multiple matches. * `matchAll`: + Pros: Finds all matches, which can be useful if you need to process each match individually. + Cons: Requires an iterator, which can be slower and more complex than a simple `match` call. **Library and Purpose** The regex library being used is built-in to JavaScript. The `match` and `matchAll` methods are part of the String prototype, making it easy to use regular expressions in string concatenation and matching. **Other Considerations** When using regular expressions, keep in mind that: * RegEx can be slow if they're too complex or contain many repeating patterns. * Using global flags (`g`) can improve performance by reducing the number of iterations needed to find all matches. * Be mindful of performance when using regex with very large strings. **Alternatives** If you need to perform regular expression matching, you might consider alternative approaches: 1. Use a dedicated library like `regex` or `rsexp`. 2. Write custom code to use the underlying regex engine. 3. Consider using other string processing algorithms that don't rely on regular expressions (e.g., string iteration, loop-based search). I hope this explanation helps you understand what's being tested in this benchmark!
Related benchmarks:
RegEx.test vs. String.includes vs. String.match (case insensitive)
RegEx.test vs RegEx.match when fails
RegEx.test (various styles) vs. String.includes vs. String.match vs. String Equality vs. String.indexOf
Reuse Regex? RegEx.test vs. String.match vs. String.search
RegEx.test vs. String.includes vs. String.match vs String.matchAll vs String.indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?