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
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); }
Tests:
Trunc Mat
trunc(17.97777, 2)
Trunc
trunc2(17.97777, 2)
dosDecimales
dosDecimales(17.97777, 2)
trunc_fixed
trunc_fixed(17.97777, 2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Trunc Mat
Trunc
dosDecimales
trunc_fixed
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 JSON data and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The `trunc-benchmark` benchmark tests four different functions that perform decimal truncation (rounding down to a specified number of decimal places) on a given number: 1. `trunc(x, posiciones = 0)`: This function takes an optional second argument `posiciones` which specifies the maximum number of decimal places to keep. 2. `trunc2(x, posiciones = 0)`: Similar to `trunc`, but uses `substr()` to extract a substring from the original string representation of `x`. 3. `dosDecimales(n, posiciones = 0)`: This function takes an optional second argument `posiciones` which specifies the maximum number of decimal places to keep. 4. `trunc_fixed(x, posiciones = 0)`: This function uses the `toFixed()` method to round down `x` to a specified number of decimal places. **Comparison** The four functions are compared in terms of performance (measured as "ExecutionsPerSecond") using different JavaScript engines and platforms. **Pros and Cons** Here's a brief overview of each approach: 1. `trunc(x, posiciones = 0)`: This implementation uses arithmetic operations to calculate the decimal part and then applies rounding logic. Pros: simple and efficient for small numbers. Cons: may not perform well for very large or very small numbers due to potential integer overflow. 2. `trunc2(x, posiciones = 0)`: Using `substr()` can be slower than arithmetic-based implementations, but it's often more reliable for handling strings with decimal points. Pros: handles string manipulation correctly, Cons: may be slower. 3. `dosDecimales(n, posiciones = 0)`: This implementation uses a regular expression to extract the decimal part from the string representation of `n`. Pros: simple and efficient for small numbers. Cons: may not perform well for very large or very small numbers due to potential regex performance issues. 4. `trunc_fixed(x, posiciones = 0)`: Using `toFixed()` can be slower than arithmetic-based implementations, but it's often more reliable for handling decimal points. Pros: handles decimal point correctly, Cons: may be slower. **Libraries and Features** * No external libraries are used in these benchmark functions. * No special JavaScript features or syntax are used beyond basic arithmetic operations and string manipulation. **Alternative Approaches** Other approaches to performing decimal truncation might include: 1. Using a dedicated decimal arithmetic library (e.g., `decimal.js`). 2. Implementing decimal truncation using bitwise operations (e.g., shifting and masking). 3. Using a Just-In-Time (JIT) compiler or other optimization techniques to improve performance. These alternatives may offer better performance, reliability, or readability, depending on the specific use case and requirements.
Related benchmarks:
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?