var str = "Test abc test test abc test test test abc test test abc"
str = str.replace(/abc/g, "replaced text");
str = str.split("abc").join("replaced text");
str = str.replace(new RegExp("abc", "g"), "replaced text");
while(str.includes("abc")){
str = str.replace("abc", "replaced text");
}
while(str.indexOf("abc") !== -1){
str = str.replace("abc", "replaced text");
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regular Expression Literal: | |
split and join | |
Regular Expression: | |
while replace includes | |
while replace indexOf |
Test name | Executions per second |
---|---|
Regular Expression Literal: | 8927917.0 Ops/sec |
split and join | 6070086.0 Ops/sec |
Regular Expression: | 4984785.5 Ops/sec |
while replace includes | 17430994.0 Ops/sec |
while replace indexOf | 17279666.0 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark that compares the performance of different approaches to replace all occurrences of a substring in a string: using regular expressions with literals, using split and join, and using while loops with includes or indexOf methods.
Comparison Approaches
There are four test cases:
str.replace(/abc/g, "replaced text");
str.split("abc").join("replaced text");
while (str.includes("abc")) { str = str.replace("abc", "replaced text"); }
while (str.indexOf("abc") !== -1) { str = str.replace("abc", "replaced text"); }
Library Usage
None of the test cases use any external libraries. However, the RegExp
object is used in the "Regular Expression Literal" and "Regular Expression:" approaches, which is a built-in JavaScript object.
Special JS Feature or Syntax
The benchmark uses JavaScript's regular expression matching syntax to match the substring "abc". There is no special JavaScript feature or syntax explicitly mentioned, but the use of g
flag in some test cases suggests using the global matching mode, which can improve performance for certain types of patterns.