const radix = '.'
const regex = /\B(?=(\d{3})+(?!\d))/g
const separator = ' '
const value = '111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
function insertSeparators () {
const parts = value.split(radix)
parts[0] = parts[0].replace(regex, separator)
return parts.join(radix)
}
insertSeparators()
const radix = '.'
const separator = ' '
const value = '111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
function insertSeparators () {
let [int, dec] = value.split(radix)
int = BigInt(int).toLocaleString()
int = int.replace(',', separator)
return [int, dec].join(radix)
}
insertSeparators()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
test1 | |
test2 |
Test name | Executions per second |
---|---|
test1 | 15340.9 Ops/sec |
test2 | 121836.0 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition json represents two individual test cases, each with its own script preparation code. Our goal is to explain what is tested in these test cases, compare different options, discuss pros and cons of those approaches, and describe any special JavaScript features or syntax used.
Test Case 1: "Just a random replace test 1"
The first test case uses the String.prototype.replace()
method with a regular expression to replace all occurrences of a specific pattern in a large string. The pattern is defined as \\B(?=(\\d{3})+(?!\\d))
, which matches any digit (0-9) that is part of a group of three digits ((\\d{3})
) followed by a negative lookahead assertion ((?!\\d)
).
Script Preparation Code
The script preparation code for this test case is empty, indicating that no additional setup or initialization is required.
Test Case 2: "Just a random replace test 2"
The second test case also uses the String.prototype.replace()
method with a regular expression to replace all occurrences of a specific pattern in a large string. However, this time, the pattern involves splitting the input string into two parts using BigInt
and then converting one part back to a string.
Script Preparation Code
The script preparation code for this test case includes some setup to split the input string into two parts using BigInt
, convert one part back to a string, and then use String.prototype.replace()
to replace the pattern.
Options Compared
In both test cases, the same regular expression is used (\\B(?=(\\d{3})+(?!\\d))
). However, the approaches differ in how they handle the large input strings:
String.prototype.replace()
with a single pass over the entire string.BigInt
, converts one part back to a string, and then uses String.prototype.replace()
.Pros and Cons
Here are some pros and cons of each approach:
BigInt
and string conversion.Special JavaScript Features or Syntax
Neither test case uses any special JavaScript features or syntax beyond regular expressions. However, Test Case 2 does use BigInt
, which is a relatively new feature introduced in ECMAScript 2020.
Other Alternatives
If the benchmark aims to optimize string replacement performance for large input strings, other alternatives could include:
string-parsing
.Keep in mind that these alternatives may introduce additional complexity and trade-offs, and should be carefully evaluated based on the specific requirements of the benchmark.