function regex (s, c) {
return (s.match(new RegExp(c, "g")) || []).length;
}
function split (s, c) {
return s.split(c).length - 1;
}
function indexOf1 (s, c) {
var i = 0, count = 0;
while ((i = s.indexOf(c, i)) >= 0) {
count++;
i++;
}
return count;
}
function indexOf2 (s, c) {
for (let i = -2, count = -1; i != -1; count++, i = s.indexOf(c, i + 1));
}
function singleChar (s, c) {
for(let i = 0, count = 0, l = s.length; i < l; count += +(c === s[i++]));
}
var s = "Maecenas sed lacus erat. Sed fringilla dui ac mollis condimentum. Suspendisse bibendum nulla eros, ut lobortis orci posuere quis. Nunc fringilla ut metus ultrices dictum. Nunc elementum feugiat leo. Ut fermentum, enim vel vehicula posuere, metus eros imperdiet risus, at elementum mauris urna et quam. Ut vehicula, velit at placerat ornare, nulla nisi finibus sapien, non elementum tellus purus in ex. Donec pharetra elit in rhoncus placerat. Sed volutpat eget justo ut dapibus. Mauris consectetur turpis ac euismod aliquam. Sed pellentesque pretium nunc, nec rhoncus leo euismod ut. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur gravida sed urna quis tempus. Nam aliquet tortor convallis libero mollis luctus.";
var c = "a";
window.result = regex(s, c);
window.result = split(s, c);
window.result = indexOf1(s, c);
window.result = indexOf2(s, c);
window.result = singleChar(s, c);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex | |
split | |
indexOf1 | |
indexOf2 | |
singleChar |
Test name | Executions per second |
---|---|
regex | 720311.9 Ops/sec |
split | 3245172.5 Ops/sec |
indexOf1 | 1459711.2 Ops/sec |
indexOf2 | 1503799.6 Ops/sec |
singleChar | 535844.1 Ops/sec |
Let's dive into the world of MeasureThat.net and analyze the provided benchmark.
Benchmark Definition JSON
The benchmark definition represents a simple task: counting the occurrence of a specific character in a given string. The benchmark is divided into four test cases, each with its own implementation:
regex(s, c)
: uses regular expressions to find all occurrences of the character c
in the string s
.split(s, c)
: splits the string s
into an array using the character c
as the delimiter and then returns the length of the resulting array minus 1 (since the split operation creates an empty string at the end).indexOf1(s, c)
: uses a loop to find all occurrences of the character c
in the string s
.indexOf2(s, c)
: uses a loop with an index increment that starts from -2 and iterates over the string s
to find all occurrences of the character c
.Options Compared
The four test cases use different approaches to achieve the same goal:
Pros and Cons
Here's a brief analysis of each approach:
Library Usage
None of the provided test cases uses an external library.
Special JavaScript Features or Syntax
The benchmark defines custom functions regex
, split
, indexOf1
, and indexOf2
using the JavaScript syntax. The window.result = ...
syntax is used to store the result in a variable, which will be executed by the browser.
Other Alternatives
If you were to rewrite this benchmark, you could consider alternative approaches like:
String.prototype.split()
, String.prototype.indexOf()
)Array.prototype.includes()
or RegExp.prototype.test()
However, for this specific benchmark, the provided custom implementations are suitable for demonstrating the different approaches to solving the problem.