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
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) }
Tests:
Trunc Mat
trunc(17.97777, 2)
Trunc
trunc2(17.97777, 2)
dosDecimales
dosDecimales(17.97777, 2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Trunc Mat
Trunc
dosDecimales
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Trunc Mat
44328808.0 Ops/sec
Trunc
17185314.0 Ops/sec
dosDecimales
24536660.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing three different approaches to truncate (round down) decimal numbers in JavaScript: 1. `trunc(x, posiciones = 0)` - This function takes two arguments: a number `x` and an optional position `posiciones`. It returns the truncated value of `x` with the specified number of positions after the decimal point. 2. `trunc2(x, posiciones = 0)` - Similar to `trunc`, but it uses string manipulation to extract the desired part of the number. 3. `dosDecimales(n, posiciones = 0)` - This function takes two arguments: a number `n` and an optional position `posiciones`. It returns the truncated value of `n` with the specified number of positions after the decimal point. **Options Compared** The three functions are being compared in terms of performance (executions per second). The results will show which approach is faster for each test case. **Pros and Cons of Each Approach** 1. `trunc(x, posiciones = 0)`: * Pros: Easy to understand and implement, uses built-in JavaScript functionality. * Cons: May be slower due to the overhead of creating a new number object. 2. `trunc2(x, posiciones = 0)`: * Pros: Can be faster since it avoids the overhead of creating a new number object. * Cons: Requires string manipulation, which can be slower than numeric operations. 3. `dosDecimales(n, posiciones = 0)`: * Pros: Uses a custom implementation that might be optimized for specific use cases. * Cons: May not be as widely supported or understood as the other two approaches. **Library and Syntax** None of the functions in this benchmark rely on any external libraries. The syntax is standard JavaScript, with no special features or syntax. **Considerations** When implementing these functions, consider the trade-offs between performance, readability, and maintainability. For example: * When using `trunc(x, posiciones = 0)`, ensure that the built-in number object is properly initialized to avoid issues. * When using `dosDecimales(n, posiciones = 0)`, test thoroughly to ensure that the custom implementation works correctly for all edge cases. **Alternatives** If you need to perform similar tasks in your code, consider these alternatives: 1. Use built-in functions like `Math.trunc()` (available in modern browsers and Node.js) or `Number.EPSILON` (a small value used by JavaScript to represent floating-point errors). 2. Implement a custom implementation using bitwise operations, but be aware that this can be error-prone and may not work correctly for all use cases. 3. Use libraries like [Big.js](https://github.com/mike-leclerc/Big.js) or [Decimal.js](https://m1sh4n.github.io/decimal-js/) to perform decimal arithmetic, but be aware that these libraries may have additional dependencies and complexity. In summary, the `trunc(x, posiciones = 0)` function uses built-in JavaScript functionality, while `trunc2(x, posiciones = 0)` relies on string manipulation. The custom implementation in `dosDecimales(n, posiciones = 0)` might be optimized for specific use cases but requires thorough testing.
Related benchmarks:
trunc-benchmark
trunc-benchmark
trunc-benchmark
trunc-benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?