Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
trunc-benchmark
(version: 0)
trunc
Comparing performance of:
Trunc Mat vs Trunc vs dosDecimales vs trunc_fixed vs trunc_pows vs quick_pows
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function dosDecimales(n) { let t=n.toString(); let regex=/(\d*.\d{0,2})/; return t.match(regex)[0]; } function trunc (x, posiciones = 0) { var s = x.toString() var l = s.length var decimalLength = s.indexOf('.') + 1 if (l - decimalLength <= posiciones){ return x } // Parte decimal del número var isNeg = x < 0 var decimal = x % 1 var entera = isNeg ? Math.ceil(x) : Math.floor(x) // Parte decimal como número entero // Ejemplo: parte decimal = 0.77 // decimalFormated = 0.77 * (10^posiciones) // si posiciones es 2 ==> 0.77 * 100 // si posiciones es 3 ==> 0.77 * 1000 var decimalFormated = Math.floor( Math.abs(decimal) * Math.pow(10, posiciones) ) // Sustraemos del número original la parte decimal // y le sumamos la parte decimal que hemos formateado var finalNum = entera + ((decimalFormated / Math.pow(10, posiciones))*(isNeg ? -1 : 1)) return finalNum } function trunc2 (x, posiciones = 0) { var s = x.toString() var l = s.length var decimalLength = s.indexOf('.') + 1 var numStr = s.substr(0, decimalLength + posiciones) return Number(numStr) } function trunc_fixed (x, posiciones = 0) { return +x.toFixed(posiciones); } var pows = new Array(32).fill().map(function(_ ,i) {return Math.pow(10,i);}); function trunc_pows (x, posiciones = 0) { var s = x.toString() var l = s.length var decimalLength = s.indexOf('.') + 1 if (l - decimalLength <= posiciones){ return x } // Parte decimal del número var isNeg = x < 0 var decimal = x % 1 var entera = isNeg ? Math.ceil(x) : Math.floor(x) // Parte decimal como número entero // Ejemplo: parte decimal = 0.77 // decimalFormated = 0.77 * (10^posiciones) // si posiciones es 2 ==> 0.77 * 100 // si posiciones es 3 ==> 0.77 * 1000 var decimalFormated = Math.floor( Math.abs(decimal) * pows[posiciones] ) // Sustraemos del número original la parte decimal // y le sumamos la parte decimal que hemos formateado var finalNum = entera + ((decimalFormated / pows[posiciones])*(isNeg ? -1 : 1)) return finalNum } function quick_pows (x, posiciones = 0) { return (x*pows[posiciones] | 0) / pows[posiciones]; } console.log(pows); console.log(trunc_pows(17.2345, 2));
Tests:
Trunc Mat
trunc(17.97777, 2)
Trunc
trunc2(17.97777, 2)
dosDecimales
dosDecimales(17.97777, 2)
trunc_fixed
trunc_fixed(17.97777, 2)
trunc_pows
trunc_pows(17.97777, 2)
quick_pows
quick_pows(17.97777, 2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Trunc Mat
Trunc
dosDecimales
trunc_fixed
trunc_pows
quick_pows
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 benchmark. **Benchmark Overview** The benchmark measures the performance of different JavaScript functions that truncate decimal numbers to a specified number of positions. The benchmarks are written in a specific order, which might affect their relative performance. **Functions Being Tested** 1. `dosDecimales(n)`: This function takes an integer `n` as input and returns its string representation with only two decimal places. 2. `trunc(x, posiciones = 0)`: This function truncates the decimal part of a number `x` to `posiciones` positions. The default value of `posiciones` is 0, which means no decimal part is truncated. 3. `trunc2(x, posiciones = 0)`: Similar to `trunc`, but it's not clear why this function exists since `dosDecimales` already achieves the same result. 4. `trunc_fixed(x, posiciones = 0)`: This function truncates the decimal part of a number `x` to `posiciones` positions and returns an integer value. 5. `trunc_pows(x, posiciones = 0)`: This function uses a mathematical approach to truncate the decimal part of a number `x` to `posiciones` positions. 6. `quick_pows(x, posiciones = 0)`: This function also truncates the decimal part of a number `x` to `posiciones` positions but uses a different algorithm. **Observations** * The `trunc2` function is not as efficient as the other functions that achieve similar results. * The `trunc_pows` and `quick_pows` functions are almost identical, with only minor differences in their implementation details. * The `dosDecimales` function seems to be a slowest among all functions. **Conclusion** The benchmark provides valuable insights into the performance of different JavaScript functions for truncating decimal numbers. Based on the results: 1. Use `trunc_pows` or `quick_pows` for optimal performance, as they are very similar and likely have negligible differences. 2. Avoid using `dosDecimales`, as it is significantly slower than the other options. 3. The existence of `trunc2` suggests that the developer was experimenting with different approaches, but it's not clear why this function wasn't refactored or optimized further. I hope this analysis helps you understand the benchmark results!
Related benchmarks:
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?