function regex(s, re) {
var r = s.match(re);
if (r) return r.length;
return 0;
}
function split(s, oc) {
return s.split(oc).length - 1;
}
function indexOf(s, oc) {
var i = 0, count = 0;
while ((i = s.indexOf(oc, i)) >= 0) {
count++;
i++;
}
return count;
}
window.result = regex("This is a pen", /is/g);
window.result = split("This is a pen", "is");
window.result = indexOf("This is a pen", "is");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Use regex | |
Use split | |
Use indexOf |
Test name | Executions per second |
---|---|
Use regex | 21631072.0 Ops/sec |
Use split | 47845420.0 Ops/sec |
Use indexOf | 34807444.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is to measure which method (regex, split, or indexOf) is the fastest for counting the occurrences of a substring in a string. The description links to Stack Overflow questions that provide context for the problem.
Script Preparation Code
The script preparation code includes three functions:
regex(s, re)
: This function takes two arguments: the input string s
and a regular expression re
. It returns the number of matches found in the string.split(s, oc)
: This function takes two arguments: the input string s
and a separator character or string oc
. It splits the string into substrings separated by oc
and returns the length of the resulting array minus one (which effectively removes the empty strings at the start and end).indexOf(s, oc)
: This function takes two arguments: the input string s
and a substring oc
. It iterates through the string to find all occurrences of oc
starting from the current position and returns the number of matches.Html Preparation Code
There is no HTML preparation code provided, which suggests that the benchmark is focused solely on JavaScript performance.
Individual Test Cases
The three test cases are:
These test cases execute each function once with a specific input string ("This is a pen") and a substring ("is").
Latest Benchmark Result
The benchmark result shows the performance metrics for each test case, including:
RawUAString
: The raw user agent string of the browser that executed the benchmark.Browser
and DevicePlatform
: Information about the browser and device platform used.OperatingSystem
: The operating system used by the browser.ExecutionsPerSecond
: The average number of executions per second for each test case.Approaches Compared
The three approaches are:
Pros and Cons
Here are some pros and cons for each approach:
Library Use
None of the test cases use external libraries.
Special JS Features
The benchmark does not appear to use any special JavaScript features beyond standard ES6 syntax. If you're interested in exploring other features, such as async/await or Promises, I can help with that!
Other Alternatives
If you'd like to explore alternative methods for counting substring occurrences, here are a few options:
These alternatives may offer different trade-offs in terms of performance, simplicity, or feature set. Let me know if you'd like to explore any of these options further!