var char = ' '
var count = 3
function formatNumber(num) {
const arr = num.toString().split('');
let it = 1;
for (let i = arr.length - 1; i >= 1; i--) {
if (it % count === 0) {
arr.splice(i, 0, char);
}
it++;
}
return arr.join('');
}
function formatNumber1bis(num) {
const arr = `${num}`.split('');
let it = 1;
for (let i = arr.length - 1; i >= 1; i--) {
if (it % count === 0) {
arr.splice(i, 0, char);
}
it++;
}
return arr.join('');
}
function formatNumber2(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, char);
}
function formatNumber2bis(num) {
return `${num}`.replace(/\B(?=(\d{3})+(?!\d))/g, char);
}
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000]
for (let i = 0; i < numbers.length; i++) {
formatNumber2(numbers[i])
}
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000]
for (let i = 0; i < numbers.length; i++) {
formatNumber(numbers[i])
}
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000]
for (let i = 0; i < numbers.length; i++) {
formatNumber2bis(numbers[i])
}
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000]
for (let i = 0; i < numbers.length; i++) {
formatNumber1bis(numbers[i])
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex test (toString) | |
code test (toString) | |
regex test ```` | |
code test ```` |
Test name | Executions per second |
---|---|
regex test (toString) | 828992.1 Ops/sec |
code test (toString) | 361758.6 Ops/sec |
regex test ```` | 843923.6 Ops/sec |
code test ```` | 366206.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Definition JSON
The provided JSON represents a benchmark definition for a JavaScript function called formatNumber
. The benchmark compares three different approaches to format numbers:
Options Compared
The benchmark compares the performance of two regex-based approaches and two code-based approaches. The options are:
formatNumber2
(Regex approach): uses a regex pattern to replace whitespace with the specified character.formatNumber2bis
(Regex approach): uses a similar regex pattern, but applied to a string template literal (${num}
) instead of a plain string.formatNumber1bis
(Code V1.1 approach): implements a custom implementation using traditional JavaScript coding style.Pros and Cons
Here are some pros and cons for each approach:
Library Usage
The String.prototype.replace()
method is used in both regex-based approaches (formatNumber2
and formatNumber2bis
). This method replaces occurrences of a pattern with a replacement string. In this case, the pattern is \\B(?=(\\d{3})+(?!\\d))
, which matches whitespace characters only between groups of three digits (with an optional digit before or after the group).
Special JavaScript Feature
The benchmark uses template literals (${num}
) in some test cases. Template literals are a feature introduced in ECMAScript 2015 that allows embedding expressions inside string literals.
Other Alternatives
If you were to implement this benchmark, you could also consider other approaches, such as:
formatNumber
function using a different language (e.g., C++ or Rust)Keep in mind that the specific implementation and optimizations will depend on your goals, target audience, and performance requirements.