const SIZE = 100000;
var chars = new Array(SIZE);
for (let index = 0; index < SIZE; index++) {
chars[index] = String.fromCodePoint(
Math.floor( Math.random() * 0x80 )
);
}
var newChars = new Array(SIZE);
const regexp = /[\x00-\x1f\x7f]/g;
for (const char of chars) {
newChars.push(char.replace(regexp, ""));
}
console.log(newChars);
for (const char of chars) {
newChars.push(char.replace(/[\x00-\x1f\x7f]/g, ""));
}
console.log(newChars);
const regexp = new RegExp("[\x00-\x1f\x7f]");
for (const char of chars) {
newChars.push(char.replace(regexp, ""));
}
console.log(newChars);
for (const char of chars) {
newChars.push(char.replace(new RegExp("[\x00-\x1f\x7f]"), ""));
}
console.log(newChars);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Outside, literal | |
Inside, literal | |
Outside, object | |
Inside, object |
Test name | Executions per second |
---|---|
Outside, literal | 90.7 Ops/sec |
Inside, literal | 96.2 Ops/sec |
Outside, object | 114.6 Ops/sec |
Inside, object | 58.9 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what's being tested.
Benchmark Definition
The benchmark measures the performance difference between three approaches when removing Unicode characters (specifically, ASCII characters with codes 0-31, 127, or 239) from an array of randomly generated Unicode strings using regular expressions.
Script Preparation Code
The script prepares two arrays: chars
and newChars
. chars
contains 100,000 random Unicode strings, while newChars
is initially empty. The script uses a loop to populate newChars
by iterating over each character in chars
.
Html Preparation Code
There is no HTML preparation code provided.
Individual Test Cases
The benchmark consists of four test cases:
regexp
) is defined as a string literal outside the loop, and then used to remove Unicode characters from each character in chars
.chars
. This test case compares the performance difference between defining the regex outside vs inside the loop.new RegExp("[\\x00-\\x1f\\x7f]")
) and used to remove Unicode characters from each character in chars
. This test case compares the performance difference between using a regular expression as a string literal vs an RegExp object.chars
.Pros and Cons of Different Approaches
Library Used
The benchmark uses the built-in String.prototype.replace()
method with a regular expression, which is implemented in C++ by the JavaScript engine. No additional libraries are required.
Special JS Feature or Syntax
No special features or syntax are used in this benchmark. It's purely focused on testing performance variations of removing Unicode characters from an array using different approaches.
Now, when looking at the latest benchmark result, we can see that:
Other Alternatives
To measure performance variations similar to this benchmark, other alternatives could include:
Keep in mind that the choice of alternative benchmark depends on the specific requirements and goals of the project.