Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
trunc-benchmark
trunc
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser:
Chrome 137
Operating system:
Windows
Device Platform:
Desktop
Date tested:
11 months ago
Test name
Executions per second
Trunc Mat
24739592.0 Ops/sec
Trunc
24462060.0 Ops/sec
dosDecimales
5304113.5 Ops/sec
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)