var str = 'really rare (Phone number hidden by company) (Email hidden by company)';
str.replace(/(\[TL_HIDDEN\])|(\[EMAIL_HIDDEN\])|(Phone number hidden by company)|(Email hidden by company)/g, "");
str.split('(Phone number hidden by company)')
.join('')
.split('(Email hidden by company)')
.join('')
.split('[TL_HIDDEN]')
.join('')
.split('[EMAIL_HIDDEN]')
.join('');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex | |
Multiple Split and Join |
Test name | Executions per second |
---|---|
Regex | 3840367.5 Ops/sec |
Multiple Split and Join | 845407.7 Ops/sec |
Let's break down what's being tested in the provided benchmark.
What is being tested?
The benchmark compares two approaches to remove hidden phone numbers and email addresses from a string:
(Phone number hidden by company)
, (Email hidden by company)
, [TL_HIDDEN]
, and [EMAIL Hidden]
. This approach is often used for text processing, pattern matching, and validation.split()
and join()
methods to remove the hidden patterns from the string. This approach involves splitting the string into substrings using specific patterns, filtering out the unwanted parts, and then joining the remaining parts back together.Options compared
These two approaches are being compared to measure their performance. The test results will show which method is faster and more efficient for this specific task.
Pros and Cons of each approach:
Library/Utility
In this case, there is no specific library or utility mentioned. The tests only rely on standard JavaScript methods: replace()
, split()
, and join()
.
Special JS feature or syntax
There are a few special features used in the benchmark:
\1
backreference is used to match the first captured group (\(Phone number hidden by company\)
). This allows the regex engine to reference previously matched groups.split()
method in both tests uses whitespace characters (\r\n
) as separators. This implies that the string contains newline characters, which are not explicitly shown in the provided code.Other alternatives
If you need to compare other approaches for removing hidden phone numbers and email addresses from a string, some alternatives could be:
replace
or escapeRegExp
, which might provide faster performance and more features.dompurify
or sanitize.js
, designed specifically for web application security.Keep in mind that these alternatives would depend on the specific requirements and constraints of your project.