var str = '11.04.09.100.98';
str.replace(/\.\d{1,3}$/, '')
str.split('.').splice(0,3).join('.')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex | |
Split and Join |
Test name | Executions per second |
---|---|
Regex | 1778194.0 Ops/sec |
Split and Join | 643555.2 Ops/sec |
Let's break down what's being tested in this benchmark.
What is being tested?
The benchmark is comparing the performance of two approaches to remove the last three digits from a string: using regular expressions (regex
) and using split
, splice
, and join
methods.
Options compared
There are two options being compared:
\\d{1,3}
) in the string and replaces them with an empty string..
as the separator, removes the first three elements using splice(0, 3)
, and then joins the remaining elements back into a string using join('.')
.Pros and cons of each approach
Other considerations
Alternatives
If you wanted to compare other approaches, some alternatives could include:
substr
or substring
In summary, this benchmark is designed to compare the performance of two approaches to remove the last three digits from a string: using regular expressions and using split
, splice
, and join
methods. The regex approach has its advantages in terms of conciseness and expressiveness, but may be slower due to compilation overhead. The split, splice, join approach is more straightforward and less error-prone, but less concise and expressive.