Script Preparation code:
x
 
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));
}
Tests:
  • utility

     
    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)
    }
  • log

     
    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)
    }
  • e

     
    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)
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    utility
    log
    e

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
Chrome 92 on Linux
View result in a separate tab
Test name Executions per second
utility 709.6 Ops/sec
log 702.9 Ops/sec
e 651.1 Ops/sec