<!--your preparation HTML ode goes here-->
const stuff = "<p>testing subscriptions app 1</p><br><p><a target=\"_blank\" rel=\"noopener noreferrer nofollow\" class=\"link\" href=\"https://{{user_application_link}}\">{{user_application_display_name}}</a></p><br><p><span data-type=\"mention\" class=\"merge-field\" data-id=\"email_signature\"></span></p><br><p><a target=\"_blank\" rel=\"noopener noreferrer\" class=\"link\" href=\"https://{{user_application_link}}\">manual tag</a></p><br><p><a target=\"_blank\" rel=\"noopener noreferrer\" class=\"link\" href=\"https://google.com\">test</a></p> {{unsubscribe}}"
stuff.split('https://').filter(Boolean).join('https://');
stuff.replace(/(https:\/\/)+/g, 'https://');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Split | |
Regex |
Test name | Executions per second |
---|---|
Split | 5694860.0 Ops/sec |
Regex | 399020096.0 Ops/sec |
This benchmark compares two different string manipulation techniques used to replace specific substrings in a longer string, specifically a URL within a block of HTML content. The two approaches being tested are:
String Split and Join Approach
stuff.split('https://').filter(Boolean).join('https://');
stuff
at each instance of the substring 'https://'
resulting in an array of strings. The filter(Boolean)
part removes any empty strings that might occur due to consecutive delimiters or leading/trailing delimiters. Finally, the join('https://')
method concatenates the array elements back into a single string, adding the delimiter back between each of the elements.Regular Expression Replace Approach
stuff.replace(/(https:\\/\\/)+/g, 'https://');
'https://'
in the string stuff
. The expression captures these occurrences and replaces them with a single instance of 'https://'
. The g
flag in the regex indicates a global search, meaning it will replace all matches in the string.From the benchmark results provided, we can see that the Regex approach significantly outperforms the Split method in terms of executions per second:
Pros:
Cons:
Pros:
Cons:
Readability vs. Performance: If performance is a critical concern (e.g., manipulating large strings or doing replacements in loops), the regex approach is typically better. For small-scale uses where clarity of intent is more important, the split and join method may suffice.
Complexity of Patterns: If the replacements needed become more complicated (not just a simple substring but patterns or conditions), regex quickly becomes the more flexible and powerful choice.
Beyond these two methods, additional alternatives for string manipulation in JavaScript could include:
String.prototype.replaceAll(): If available in the environment, this method allows for simpler syntax for replacing all instances of a substring without needing to create a regex.
Custom Iterative Functions: For very specific or complex string manipulation tasks, a custom iterative function might be used—though this would typically not be as performant or succinct as regex.
In conclusion, the choice between these methods mainly hinges on the specific use case: if you prioritize performance and have proficiency with regex, the regular expression replace method is preferable. For simpler cases or for developers less comfortable with regex, the split and join method remains a valid choice.