Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regexp Greedy vs lazy cost
(version: 0)
Comparing performance of:
Lazy & good string vs Lazy & bad string vs Greedy & good string vs Greedy & bad string
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
str = 'Crew across the rivers' badStr = 'A crew boards the ship' searchFor = [...'chris'] lazyRE = RegExp(searchFor.join('.*?'), 'i') greedyRE = RegExp(searchFor.join('.*'), 'i')
Tests:
Lazy & good string
lazyRE.test(str)
Lazy & bad string
lazyRE.test(badStr)
Greedy & good string
greedyRE.test(str)
Greedy & bad string
greedyRE.test(badStr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lazy & good string
Lazy & bad string
Greedy & good string
Greedy & bad string
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 break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches for matching strings using regular expressions (RegEx) in JavaScript: 1. Lazy Matching (`lazyRE`) 2. Greedy Matching (`greedyRE`) **Lazy Matching (`lazyRE`)** In lazy matching, the engine only attempts to match the first part of the string and then backtracks if the entire string does not match. This approach is more efficient because it avoids unnecessary work. **Pros:** * Faster execution time * Reduced memory usage * Less CPU cycles used **Cons:** * May produce incorrect results if the input string has a specific pattern that requires multiple matches to find * Can be slower for certain types of data (e.g., large datasets) **Greedy Matching (`greedyRE`)** In greedy matching, the engine attempts to match as much of the string as possible before backtracking. This approach is more intuitive but can lead to performance issues. **Pros:** * More straightforward to implement and understand * Can be faster for certain types of data (e.g., small datasets) **Cons:** * Slower execution time * Higher memory usage * More CPU cycles used **Library: RegExp** The `RegExp` library is a built-in JavaScript library that provides support for regular expressions. It's used in both lazy and greedy matching approaches to define the search patterns. In the benchmark, the `RegExp` constructor is used to create instances of `lazyRE` and `greedyRE`, which are then used to perform the actual string matching. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax. It's designed to be a straightforward comparison of two well-known approaches for regular expression matching. **Alternative Approaches** Other approaches to regular expression matching in JavaScript include: 1. Use `String.prototype.match()` or `String.prototype.search()`, which are built-in methods that can be used for matching strings. 2. Implement custom string matching algorithms using bitwise operations and bit flags (e.g., UTF-8 flag). 3. Use third-party libraries like `regex-optimization` or `regex-perf`. Keep in mind that the best approach depends on the specific use case, data type, and performance requirements. **Benchmark Preparation Code** The provided code prepares the test strings: * `str`: A string containing the phrase "Crew across the rivers". * `badStr`: A string containing a similar but different phrase ("A crew boards the ship"). * The search string is created by concatenating an array of characters using `'.' + [...'chris']`. * `lazyRE` and `greedyRE` are created using the prepared search strings and the 'i' flag for case-insensitive matching. The benchmark code then performs four test cases: 1. Testing `lazyRE` with both good (`str`) and bad (`badStr`) input strings. 2. Testing `greedyRE` with both good (`str`) and bad (`badStr`) input strings. These test cases help determine which approach is faster for each type of string match.
Related benchmarks:
regex vs trim test
Regexp Greedy vs lazy cost v2
RegEx.test vs. String.includes vs. String.match (with multiple strings)
includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?