var str = '124961925861925691256981625';
var r1 = /\D/g;
var r2 = /\D+/g;
str.replace(r1, '');
str.replace(r2, '');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
r1 | |
r2 |
Test name | Executions per second |
---|---|
r1 | 2262306.8 Ops/sec |
r2 | 2293358.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested on MeasureThat.net.
Benchmark Definition
The benchmark definition is a simple JavaScript code that defines two regular expressions: r1
and r2
. The purpose of these regular expressions is to match any non-digit characters (\\D
) in a string.
Regular Expressions
r1 = /\\D/g;
: This regular expression matches any sequence of non-digit characters (\\D
) globally (g
) in the input string.r2 = /\\D+/g;
: This regular expression matches any sequence of one or more non-digit characters (\\D+
) globally (g
) in the input string.Options compared
The benchmark is comparing the performance of two different approaches:
r1
: Replaces all non-digit characters with an empty string, effectively removing them from the input string.r2
: Replaces one or more non-digit characters with an empty string, also removing them from the input string.Pros and Cons
r1
(single character replacement):r2
(multiple character replacement):Library
The benchmark doesn't explicitly use any libraries. However, the regular expressions themselves are built into JavaScript's ECMAScript specification, which is implemented by most modern browsers.
Special JS feature/Syntax
There are no special JavaScript features or syntaxes used in this benchmark. The code is written in standard JavaScript and should be compatible with most modern browsers.
Alternative approaches
Other alternatives to these two approaches could include:
r = /\\D+|\\D/g;
which matches either one or more non-digit characters.String.prototype.replace()
with an array of replacements.Benchmark Preparation Code
The benchmark preparation code defines two variables: str
and the regular expressions r1
and r2
. The variable str
contains a sample string containing non-digit characters. The regular expression patterns are defined as strings, which can be executed using JavaScript's built-in RegExp constructor.
Individual test cases
Each individual test case represents a separate benchmark that tests the performance of either r1
or r2
. The Benchmark Definition
property specifies the code to be tested, and the Test Name
property provides a label for each test case.