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.97, 2)
dosDecimales
dosDecimales(17.97)
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 provided benchmark definition and individual test cases to understand what's being tested. **Benchmark Definition** The `trunc` function is designed to truncate a decimal number to a specified position. The input `x` is a number, and the optional second argument `posiciones` specifies the position to which the decimal part should be truncated. Here's a high-level overview of what the `trunc` function does: 1. Convert the input number `x` to a string. 2. Find the index of the decimal point (`.`) in the string. 3. If the length of the string minus the index of the decimal point is less than or equal to the specified position, return the original number. 4. Otherwise: * Extract the integer part and the fractional part (decimal) from the input number `x`. * Format the fractional part as a number with the specified position by multiplying it by 10 raised to that power (`Math.pow(10, posiciones)`). * Subtract the original fractional part from the input number `x` and add the formatted fractional part. The result is returned. The `dosDecimales` function appears to be an alternative implementation of a similar functionality, but its name suggests it might be designed to handle decimal numbers differently. However, upon closer inspection, we see that it's actually very similar to the `trunc` function, with some minor differences in variable names and logic. **Test Cases** There are two individual test cases: 1. `trunc(17.97, 2)`: Tests the `trunc` function with a decimal number (17.97) and a specified position for truncation (2). 2. `dosDecimales(17.97)`: Tests the `dosDecimales` function with a decimal number (17.97), but without specifying a truncation position. **Options Compared** The two test cases compare the performance of the `trunc` and `dosDecimales` functions when: * Truncating to a fixed position (2 in this case) * Using a different implementation for handling decimal numbers **Pros and Cons** **Trunc Function:** Pros: * Clear logic for truncation * Uses built-in JavaScript methods (e.g., `Math.pow`) to perform calculations Cons: * Might be slower due to the overhead of string manipulation and finding the decimal point index **Dos Decimales Function:** Pros: * Possibly more efficient due to fewer operations required for formatting the fractional part * Less logic, which can make it easier to maintain Cons: * Logic is less clear compared to the `trunc` function * Might not handle edge cases as well **Other Considerations** * The use of `Math.pow(10, posiciones)` in both functions suggests that they are designed to work with decimal numbers. * The `dosDecimales` function uses a variable name `decimalFormated`, which might be an attempt to make the code more readable or maintainable. **Alternatives** Other alternatives for implementing truncation functionality could include: * Using a library like `decimal.js` to handle precise decimal arithmetic * Implementing a different algorithm, such as using binary search to find the truncated decimal part In summary, the provided benchmark definition and test cases compare the performance of two functions (`trunc` and `dosDecimales`) that implement truncation functionality for decimal numbers. The `trunc` function appears to have clearer logic, while the `dosDecimales` function might be more efficient due to its simpler implementation.
Related benchmarks:
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?