const regex = /(00|11|22|33|44|55|66|77|88|99)/;
let num = 3298;
num = num + 1;
let aux = (num).toString();
while (regex.test(aux)) {
num += 1;
aux = num.toString();
}
let num = 3298;
const consecutiveChars = function(num) {
const arr = num.toString().split('');
for (let idx = 0; idx < arr.length - 1; idx++) {
if (arr[idx] == arr[idx + 1])
return true;
}
return false;
}
num = num + 1;
while (consecutiveChars(num)) {
num++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex | |
loop |
Test name | Executions per second |
---|---|
regex | 269905.5 Ops/sec |
loop | 111554.7 Ops/sec |
Overview of the Benchmark
The provided benchmark is designed to compare the performance of two approaches: using a regular expression (regex
) and a loop-based approach, in this case, a function that checks for consecutive characters in a string.
Options Compared
The benchmark compares two options:
aux
string is equal to the next one.aux
string are equal.Pros and Cons of Each Approach
Library and Its Purpose
There is no explicit library mentioned in the benchmark definition or test cases. However, the test()
function used in the regex approach is a part of the built-in JavaScript String prototype, which provides a convenient way to perform string matching with regular expressions.
Special JS Feature or Syntax
The benchmark uses modern JavaScript features, such as:
consecutiveChars
function.aux
variable.However, there are no special ES6+ features like classes, generators, or async/await used in this benchmark.
Other Alternatives
Some alternative approaches that could be tested in a similar benchmark include:
for
loop.These alternatives would require modifications to the benchmark definition and test cases to accurately reflect their performance characteristics.