Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
trunc-benchmark
(version: 0)
trunc
Comparing performance of:
trunc vs dosDecimales
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 }
Tests:
trunc
trunc(17.97777777, 2)
dosDecimales
dosDecimales(17.97777777, 2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
trunc
dosDecimales
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 break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The provided JSON defines two JavaScript microbenchmarks: 1. `trunc`: A custom function that truncates a decimal number to a specified position (default is 0). 2. `dosDecimales`: Another custom function that extracts the first `n` decimal digits of a number, where `n` is the second argument. **Options being compared** The benchmarks are comparing the performance of two approaches: 1. `trunc`: The original implementation provided in the benchmark definition. 2. `dosDecimales`: A modified version of the custom function that extracts the first `n` decimal digits of a number. **Pros and Cons** * **`trunc`**: + Pros: Simple to understand, straightforward logic. + Cons: May not be as efficient as other approaches due to string manipulation and multiple iterations. * **`dosDecimales`**: + Pros: More efficient than `trunc`, as it uses mathematical operations to extract the desired digits instead of string manipulation. + Cons: Logic is more complex, which may make it harder to understand for some developers. **Library usage** Neither benchmark defines any external libraries, but they do use custom functions (`dosDecimales` and `trunc`). These functions are specific to the benchmark and are not part of any standard JavaScript library. **Special JS feature or syntax** There is no special JavaScript feature or syntax used in these benchmarks. They only utilize basic arithmetic operations, string manipulation, and conditional statements. **Other alternatives** * Another approach could be using built-in JavaScript methods like `toFixed()` or `toExponential()`, which can also truncate numbers to a specified position. However, the performance of these built-ins may vary depending on the browser and version. * Additionally, some implementations might use a different approach, such as using a finite arithmetic library (e.g., for high-precision calculations) or exploiting hardware-specific instructions (e.g., SIMD operations). The choice between `trunc` and `dosDecimales` ultimately depends on the specific requirements of the application, such as performance, code readability, and maintainability.
Related benchmarks:
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?