const site = "archive.test.org";
const regex = new RegExp("https?:\/\/" + site + "\/");
for (let k = 0; k < 10000; k++) {
"https://archive.test.org/board/".replace(regex, "");
}
const site = "archive.test.org";
for (let k = 0; k < 10000; k++) {
"https://archive.test.org/board/".replace("https://", "").replace("http://", "").replace(site).replace("/");
}
const site = "archive.test.org";
for (let k = 0; k < 10000; k++) {
let s = "https://archive.test.org/board/";
s = s.substring(4);
if(s.startsWith("s")) {
s = s.substring(1);
}
s = s.substring(4 + site.length);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex | |
multiple replaces | |
sneaky |
Test name | Executions per second |
---|---|
regex | 2282.7 Ops/sec |
multiple replaces | 487.5 Ops/sec |
sneaky | 15270.5 Ops/sec |
Measuring performance differences between different approaches to achieve the same goal is crucial in software development, and tools like MeasureThat.net help bridge this gap by providing an environment for testing various algorithms under controlled conditions.
Let's break down each test case:
"regex"
Test Case:
regex
) to replace a specific pattern ("https://") with an empty string in the input string."multiple replaces" Test Case:
replace()
calls, each targeting a different part of the input string: "https://", "http://", and "/". It first removes the protocol part ("https://" and "http://"), then removes the domain part, leaving only the path.Comparison Options:
Pros and Cons:
Library Used:
None mentioned directly in these test cases. However, regular expressions (RegExp
) are used throughout, which are part of JavaScript's built-in library.
Special JS Feature/Syntax:
The examples use standard JavaScript syntax with some advanced string manipulation techniques like substring()
and regular expressions (RegExp
). There isn't a specific feature being highlighted or tested in these cases, but rather different approaches to achieving the same result.
Alternatives:
String.prototype.replace()
with a callback function that repeats the replacement process multiple times.In conclusion, these benchmark tests illustrate different strategies for achieving specific string manipulation goals in JavaScript, highlighting both straightforward and potentially performance-critical differences.