Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex collapse vs for loop
(version: 1)
Collapse function removes adjacent duplicates in the string
Comparing performance of:
Regex inplace replace vs for loop
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var string = "aaaaabaaaaa"; var string1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
Tests:
Regex inplace replace
string.replace(/(?:(\w)(?:\1)*)/g, "$1") string1.replace(/(?:(\w)(?:\1)*)/g, "$1")
for loop
const collapse = (string) => { let final = "", prev = ""; for (let i = 0, len = string.length, char; i < len; i++) { if ((char = string[i]) !== prev) { final += (prev = char); } } return final; }; collapse(string); collapse(string1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex inplace replace
for loop
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):
**Overview** The provided JSON represents a JavaScript benchmark, specifically designed to compare the performance of two approaches: using regular expressions for string replacement and a custom `for` loop implementation. **Benchmark Definition** The main test case is defined in the `Benchmark Definition` section: ```json "string.replace(/(?:(\\w)(?:\\1)*)/g, \"$1\")\r\nstring1.replace(/(?:(\\w)(?:\\1)*)/g, \"$1\")" ``` This code removes adjacent duplicates from a given string using regular expressions. The `/` characters denote the start of the regex pattern. **Options Compared** There are two main options compared in this benchmark: ### 1. Regular Expressions (Regex) * **Pros:** + Concise and readable syntax. + Efficient for large datasets, as it uses optimized string searching algorithms. * **Cons:** + Can be slow for small or simple strings due to the overhead of regex parsing. + May have performance issues if the regex pattern is complex or has many groups. ### 2. Custom `for` Loop * **Pros:** + Typically faster for small or simple strings, as it avoids the overhead of regex parsing. + Easy to understand and optimize for specific use cases. * **Cons:** + Less concise and readable syntax compared to regular expressions. + Requires manual management of loop iterations and variable assignments. **Library Usage** The `string.replace()` method uses the `String.prototype.replace()` method, which is a built-in JavaScript function. This method replaces occurrences of a specified pattern in a string with another value. The regex pattern `/(?:(\\w)(?:\\1)*)/g` is used to match adjacent duplicates. **Special JS Feature/Syntax** There are no special JS features or syntaxes used in this benchmark. It only employs standard JavaScript functions and data types. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Using `Array.prototype.filter()`**: Instead of removing adjacent duplicates, you can use `Array.prototype.filter()` to create an array with unique elements. ```javascript const result = [...new Set(string.split(''))]; ``` This approach has different performance characteristics compared to the regular expression method. * **Using `Map` or `Set` objects**: You can also use `Map` or `Set` objects to store unique values and then concatenate them into a string. ```javascript const map = new Map(); for (let char of string) { if (!map.has(char)) { map.set(char, true); } } const result = Array.from(map.keys()).join(''); ``` These alternatives have different performance profiles compared to the regular expression method and custom `for` loop implementation.
Related benchmarks:
includes vs indexOf (Array and String)
IndexOf vs Includes in Larger string
Data duplication
Data duplication (single run)
Set vs Map vs Object for lookups
Comments
Confirm delete:
Do you really want to delete benchmark?