/* André Antunes da Cunha - hello world! */
var negative = /<[^>]*>/g;
var lazy = /<.*?>/g;
var input =
"hello <01> hello <02> hello <03> hello <04> hello <05> hello <06> hello <07> hello <08> hello <09> hello <10> " +
"hello <11> hello <12> hello <13> hello <14> hello <15> hello <16> hello <17> hello <18> hello <19> hello <20> " +
"hello";
[input.matchAll(negative)]; /* Array.from(input.matchAll(negative)); */
[input.matchAll(lazy)]; /* Array.from(input.matchAll(lazy)); */
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Negative | |
Lazy (non-greedy) |
Test name | Executions per second |
---|---|
Negative | 1105535.1 Ops/sec |
Lazy (non-greedy) | 1138304.8 Ops/sec |
Let's break down the provided JSON and explain what's being tested in this JavaScript microbenchmark.
Benchmark Definition
The benchmark is designed to compare the performance of two regular expression (RegExp) patterns: one with a negative lookahead (negative
) and another with a lazy (non-greedy) quantifier (lazy
).
Options Compared
Two options are compared:
<[^>]*>
) to match any character that is not >
(the closing angle bracket). The g
flag at the end of the pattern makes it global, so it matches all occurrences in the input string.<.*?
) to match any character that is not >
. The ?
quantifier makes the preceding character (in this case, <
) as few times as possible, while still matching.Pros and Cons of Each Approach
Library
There is no explicit library mentioned in the benchmark definition. However, the use of matchAll
and Array.from
suggests that the benchmark is targeting modern JavaScript environments (ES6+).
Special JS Feature or Syntax
This benchmark does not specifically test any special JavaScript features or syntax beyond regular expression patterns.
Other Considerations
When comparing these two approaches, it's essential to consider the following factors:
Alternatives
If you were to create an alternative benchmark for regular expression performance, you could consider testing other common scenarios, such as:
match()
vs. matchAll()
).+
, *
, ?
) or character classes.Keep in mind that the choice of benchmark scenario ultimately depends on your specific use case and goals.