Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
get precision from number string
(version: 5)
with ignoring trailing zeroes, the function should return the precision (decimal places) of a number (decimal or non-decimal)
Comparing performance of:
utility vs log vs e
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
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>
Run tests (3)
Previous results
Fork
Test case name
Result
utility
log
e
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `removeTrailingZeroAndDots` 2. `getLog10Precision` 3. `getEPrecision` **Options Compared** The three functions differ in their approach to calculate precision: 1. **`removeTrailingZeroAndDots`**: This function removes trailing zeros and decimal points from the input string, then returns the length of the resulting string as the precision. 2. **`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. 3. **`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`: + Pros: simple and straightforward + Cons: may not work correctly for non-numeric input or very large numbers * `getLog10Precision`: + Pros: robust and accurate, but slower due to logarithm calculation + Cons: requires numerical computations, which can be slow * `getEPrecision`: + Pros: fast and efficient, but may not be accurate for all inputs + Cons: relies on an estimate, which may not always produce the correct result **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: * Using regular expressions to remove trailing zeroes and decimal points * Utilizing the `decimal` library for precise decimal arithmetic * Implementing a more advanced algorithm, such as using the `big.js` library for large numbers Keep 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: 1. `getEPrecision` 2. `removeTrailingZeroAndDots` 3. `getLog10Precision` The average executions per second for each test case are: * `utility`: 709.603 * `log`: 702.890 * `e`: 651.056 These 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.
Related benchmarks:
toFixed vs Math.round() - result as a number
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs MDN round_to_precision
Precision rounding
parse float
Comments
Confirm delete:
Do you really want to delete benchmark?