var str = 'really rare [TL_HIDDEN] (Phone number hidden by company) (Email hidden by company) [EMAIL_HIDDEN]';
str.split('(Phone number hidden by company)')
.join('')
.split('(Email hidden by company)')
.join('').replace(/(\[TL_HIDDEN\])|(\[EMAIL_HIDDEN\])/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 |
---|---|
Split and regex replace | |
Multiple Split and Join |
Test name | Executions per second |
---|---|
Split and regex replace | 981866.1 Ops/sec |
Multiple Split and Join | 711681.4 Ops/sec |
Let's break down the benchmark test.
What is being tested?
The benchmark is comparing two approaches to process a specific string:
Options being compared
The benchmark is comparing two approaches:
str.split('(Phone number hidden by company)')\r\n.join('')
), and then further splitting and joining to remove the hidden parts.Pros and cons of each approach
Library usage
The benchmark uses the replace()
method with a regular expression pattern. This suggests that the JavaScript engine being tested is capable of executing regular expressions efficiently.
Special JS feature or syntax
There are no special features or syntaxes mentioned in the test cases, so we can consider them to be standard JavaScript code.
Other alternatives
While not explicitly mentioned in this benchmark, other approaches could include:
Intl
API for internationalized string manipulation.Benchmark preparation code
The script preparation code is simple, creating a sample string with hidden phone number and email addresses. The HTML preparation code is empty, suggesting that this benchmark only focuses on JavaScript performance and does not require any specific web page rendering or layout considerations.
Overall, this benchmark test evaluates the performance of different approaches to processing strings with embedded data using JavaScript's built-in methods and regular expressions.