function generateRandomDecimalInRangeFormatted(min, max) {
let value = (Math.random() * (max - min + 1)) + min;
return Number.parseFloat(value).toFixed(Math.random() * 10);
}
var size = 100;
var arr = [];
for (i = 0; i < size; i++) {
arr.push(generateRandomDecimalInRangeFormatted(0, 1));
}
function removeTrailingZeroAndDots(
value,
removeDots = false
) {
if (!value) return '';
let result = typeof value === 'number' ? String(value) : value;
result = result
// Remove decimal 0. `1.000` => `1.`, `1.100` => `1.1`, `1.000` => `1.0`
.replace(/(\.\d*[^0])0*$/, '$1')
// Remove useless decimal. `1.0` => `1.` or `1`, `1.` => `1`
.replace(/\.0*$/, removeDots ? '' : '.');
return result;
};
function getPrecision(str) {
return removeTrailingZeroAndDots(str)?.split('.')[1]?.length || 0
}
for (i = 0; i < size; i++) {
const precision = getPrecision(arr[i]);
console.log(precision)
}
function getLog10Precision(str) {
const precision = Math.log10(Number(str)) * -1;
return precision;
}
for (i = 0; i < size; i++) {
const precision = getLog10Precision(arr[i]);
console.log(precision)
}
function getEPrecision(value) {
const num = typeof value === 'string' ? +value : value
if (!isFinite(num)) return 0;
var e = 1, p = 0;
while (Math.round(num * e) / e !== num) { e *= 10; p++; }
return p;
}
for (i = 0; i < size; i++) {
const precision = getEPrecision(arr[i]);
console.log(precision)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
utility | |
log | |
e |
Test name | Executions per second |
---|---|
utility | 709.6 Ops/sec |
log | 702.9 Ops/sec |
e | 651.1 Ops/sec |
Let's dive into the explanation of what is being tested on MeasureThat.net.
Benchmark Definition
The benchmark definition provides the context for the test cases. In this case, we're testing three different functions to determine the precision (decimal places) of numbers in strings, ignoring trailing zeroes and decimal points. The functions are:
removeTrailingZeroAndDots
getLog10Precision
getEPrecision
Options Compared
The three functions differ in their approach to calculate precision:
removeTrailingZeroAndDots
: This function removes trailing zeros and decimal points from the input string, then returns the length of the resulting string as the precision.getLog10Precision
: This function uses the logarithm base 10 to calculate the precision by converting the number to a string, taking the logarithm, and multiplying by -1.getEPrecision
: This function estimates the precision using Euler's totient function, which counts the number of positive integers up to a given number that are relatively prime to that number.Pros and Cons
Here's a brief summary of each approach:
removeTrailingZeroAndDots
:getLog10Precision
:getEPrecision
:Library Usage
None of the functions use external libraries.
Special JavaScript Features/Syntax
There are no special JavaScript features or syntax mentioned in this benchmark.
Other Alternatives
If you were to implement a new function for calculating precision, other alternatives could include:
decimal
library for precise decimal arithmeticbig.js
library for large numbersKeep in mind that each alternative has its own trade-offs in terms of performance, accuracy, and complexity.
The benchmark results show that the order of execution is:
getEPrecision
removeTrailingZeroAndDots
getLog10Precision
The average executions per second for each test case are:
utility
: 709.603log
: 702.890e
: 651.056These results suggest that getEPrecision
is the fastest, followed by removeTrailingZeroAndDots
, and then getLog10Precision
. However, please note that these results are specific to this benchmark and may not be representative of other use cases or implementations.