var re = /./gi;
function mapReplace(value) {
return value.split("").map((x, index, arr) => "*").join("")
}
function makeArrayReplace(value) {
return (new Array(value.length)).fill("*").join("");
}
function regexReplace(value) {
return value.replace(re, "*");
}
const input = "abcdefghijklmnopqrstuvwxyz";
mapReplace(input);
const input = "abcdefghijklmnopqrstuvwxyz";
regexReplace(input);
const input = "abcdefghijklmnopqrstuvwxyz";
makeArrayReplace(input);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map replace | |
Regex replace | |
Make array replace |
Test name | Executions per second |
---|---|
Map replace | 1247872.2 Ops/sec |
Regex replace | 1164045.8 Ops/sec |
Make array replace | 1995265.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark definition is a JSON object that describes the test scenario:
{
"Name": "Asterisk map replace vs stored regex replace vs make an array replace",
"Description": null,
"Script Preparation Code": "...",
"Html Preparation Code": null
}
The script preparation code provides three different functions:
mapReplace(value)
: This function replaces each character in the input string with a star (*
) by using the split()
method to split the string into an array, mapping over the array to replace each element with a star, and then joining the array back into a string.makeArrayReplace(value)
: This function creates a new array with the same length as the input string, fills it with stars (*
), and then joins the array back into a string.regexReplace(value)
: This function uses a regular expression to replace each character in the input string with a star (*
) using the replace()
method.Individual Test Cases
The benchmark definition includes three individual test cases:
Each test case has its own executable code snippet:
const input = "abcdefghijklmnopqrstuvwxyz";
mapReplace(input);
const input = "abcdefghijklmnopqrstuvwxyz";
regexReplace(input);
const input = "abcdefghijklmnopqrstuvwxyz";
makeArrayReplace(input);
These test cases are designed to execute each of the three functions and measure their performance.
Pros and Cons
Here's a brief analysis of the pros and cons of each approach:
mapReplace(value)
: This function is using array manipulation, which can be slower than other approaches.makeArrayReplace(value)
: This function creates a new array with the same length as the input string, fills it with stars (*
), and then joins the array back into a string.regexReplace(value)
: This function uses regular expressions to replace each character in the input string with a star (*
).Library Usage
None of the test cases use any external libraries. However, the regexReplace(value)
function relies on built-in JavaScript regular expression support.
Special JS Features or Syntax
There's no special JavaScript feature or syntax being used in this benchmark beyond the standard language features mentioned earlier (e.g., array manipulation, string concatenation).
Other Alternatives
If you were to reimplement this benchmark using alternative approaches, some options might include:
*
).mapReplace(value)
function using a language construct like reduce()
or forEach()
.Keep in mind that rewriting the benchmark with new approaches might require significant changes to the code and may not necessarily provide better results. The existing implementation is likely optimized for the specific use case and hardware architecture.