Last active
January 24, 2023 17:35
-
-
Save amrali-eg/31ec3a8b3d22bd840f8e6822e681a3ac to your computer and use it in GitHub Desktop.
Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2 #jsbench (http://jsbench.github.io/#31ec3a8b3d22bd840f8e6822e681a3ac) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"/> | |
| <title>Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2 #jsbench</title> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
| <script src="./suite.js"></script> | |
| </head> | |
| <body> | |
| <h1>Open the console to view the results</h1> | |
| <h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| (function (factory) { | |
| if (typeof Benchmark !== "undefined") { | |
| factory(Benchmark); | |
| } else { | |
| factory(require("benchmark")); | |
| } | |
| })(function (Benchmark) { | |
| var suite = new Benchmark.Suite; | |
| Benchmark.prototype.setup = function () { | |
| // Solution 1 | |
| var DecimalPrecision = (function() { | |
| if (Math.sign === undefined) { | |
| Math.sign = function(x) { | |
| return ((x > 0) - (x < 0)) || +x; | |
| }; | |
| } | |
| if (Math.trunc === undefined) { | |
| Math.trunc = function(v) { | |
| return v < 0 ? Math.ceil(v) : Math.floor(v); | |
| }; | |
| } | |
| var decimalAdjust = function(type, num, decimalPlaces) { | |
| var shift = function(value, exponent) { | |
| value = (value + 'e').split('e'); | |
| return +(value[0] + 'e' + (+value[1] + exponent)); | |
| }; | |
| var n = type === 'round' ? Math.abs(num) : num; | |
| var r = shift(Math[type](shift(n, +decimalPlaces)), -decimalPlaces); | |
| return type === 'round' ? Math.sign(num) * r : r; | |
| }; | |
| return { | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces); | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| return decimalAdjust('ceil', num, decimalPlaces); | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| return decimalAdjust('floor', num, decimalPlaces); | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| return decimalAdjust('trunc', num, decimalPlaces); | |
| }, | |
| // Format using fixed-point notation | |
| toFixed: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); | |
| } | |
| }; | |
| })(); | |
| // Solution 2 | |
| var DecimalPrecision2 = (function() { | |
| if (Number.EPSILON === undefined) { | |
| Number.EPSILON = Math.pow(2, -52); | |
| } | |
| if (Math.trunc === undefined) { | |
| Math.trunc = function(v) { | |
| return v < 0 ? Math.ceil(v) : Math.floor(v); | |
| }; | |
| } | |
| var isRound = function(num, decimalPlaces) { | |
| //return decimalPlaces >= 0 && | |
| // +num.toFixed(decimalPlaces) === num; | |
| var p = Math.pow(10, decimalPlaces); | |
| return Math.round(num * p) / p === num; | |
| }; | |
| var decimalAdjust = function(type, num, decimalPlaces) { | |
| if (isRound(num, decimalPlaces)) | |
| return num; | |
| var p = Math.pow(10, decimalPlaces); | |
| var e = Number.EPSILON * num * p; | |
| return Math[type]((num * p) + e) / p; | |
| }; | |
| return { | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces); | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| return decimalAdjust('ceil', num, decimalPlaces); | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| return decimalAdjust('floor', num, decimalPlaces); | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| return decimalAdjust('trunc', num, decimalPlaces); | |
| }, | |
| // Format using fixed-point notation | |
| toFixed: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); | |
| } | |
| }; | |
| })(); | |
| // Solution 3 | |
| var DecimalPrecision3 = (function() { | |
| if (Math.sign === undefined) { | |
| Math.sign = function(x) { | |
| return ((x > 0) - (x < 0)) || +x; | |
| }; | |
| } | |
| if (Math.trunc === undefined) { | |
| Math.trunc = function(v) { | |
| return v < 0 ? Math.ceil(v) : Math.floor(v); | |
| }; | |
| } | |
| // Eliminate floating-point inaccuracies. | |
| var strip = function(num) { | |
| return parseFloat(num.toPrecision(15)); | |
| }; | |
| var decimalAdjust = function(type, num, decimalPlaces) { | |
| var n = type === 'round' ? Math.abs(num) : num; | |
| var p = Math.pow(10, decimalPlaces); | |
| var r = Math[type](strip(n * p)) / p; | |
| return type === 'round' ? Math.sign(num) * r : r; | |
| }; | |
| return { | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces); | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| return decimalAdjust('ceil', num, decimalPlaces); | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| return decimalAdjust('floor', num, decimalPlaces); | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| return decimalAdjust('trunc', num, decimalPlaces); | |
| }, | |
| // Format using fixed-point notation | |
| toFixed: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); | |
| } | |
| }; | |
| })(); | |
| // Solution 4 | |
| var DecimalPrecision4 = (function() { | |
| if (Math.sign === undefined) { | |
| Math.sign = function(x) { | |
| return ((x > 0) - (x < 0)) || +x; | |
| }; | |
| } | |
| if (Math.trunc === undefined) { | |
| Math.trunc = function(v) { | |
| return v < 0 ? Math.ceil(v) : Math.floor(v); | |
| }; | |
| } | |
| var toPrecision = function(num, significantDigits) { | |
| // Return early for ±0, NaN and Infinity. | |
| if (!num || !Number.isFinite(num)) | |
| return num; | |
| var e = Math.floor(Math.log10(Math.abs(num))); | |
| var p = Math.pow(10, significantDigits - e - 1); | |
| return Math.round(num * p) / p; | |
| }; | |
| // Eliminate floating-point inaccuracies. | |
| var strip = function(num) { | |
| return toPrecision(num, 15); | |
| }; | |
| var decimalAdjust = function(type, num, decimalPlaces) { | |
| var n = type === 'round' ? Math.abs(num) : num; | |
| var p = Math.pow(10, decimalPlaces); | |
| var r = Math[type](strip(n * p)) / p; | |
| return type === 'round' ? Math.sign(num) * r : r; | |
| }; | |
| return { | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces); | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| return decimalAdjust('ceil', num, decimalPlaces); | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| return decimalAdjust('floor', num, decimalPlaces); | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| return decimalAdjust('trunc', num, decimalPlaces); | |
| }, | |
| // Format using fixed-point notation | |
| toFixed: function(num, decimalPlaces) { | |
| return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); | |
| } | |
| }; | |
| })(); | |
| }; | |
| suite.add("Solution 1 (Exponential notation)", function () { | |
| /** Solution 1 (Exponential notation) **/ | |
| DecimalPrecision.round(0.5, 0); | |
| DecimalPrecision.round(-0.5, 0); | |
| DecimalPrecision.ceil(1e-8, 2); | |
| DecimalPrecision.floor(1e-8, 2); | |
| DecimalPrecision.round(5.12, 1); | |
| DecimalPrecision.round(-5.12, 1); | |
| DecimalPrecision.ceil(5.12, 1); | |
| DecimalPrecision.ceil(-5.12, 1); | |
| DecimalPrecision.floor(5.12, 1); | |
| DecimalPrecision.floor(-5.12, 1); | |
| DecimalPrecision.trunc(5.12, 1); | |
| DecimalPrecision.trunc(-5.12, 1); | |
| DecimalPrecision.round(1.005, 2); | |
| DecimalPrecision.round(39.425, 2); | |
| DecimalPrecision.round(-1.005, 2); | |
| DecimalPrecision.round(-39.425, 2); | |
| DecimalPrecision.ceil(9.130, 2); | |
| DecimalPrecision.ceil(65.180, 2); | |
| DecimalPrecision.ceil(-2.260, 2); | |
| DecimalPrecision.ceil(-18.150, 2); | |
| DecimalPrecision.floor(2.260, 2); | |
| DecimalPrecision.floor(18.150, 2); | |
| DecimalPrecision.floor(-9.130, 2); | |
| DecimalPrecision.floor(-65.180, 2); | |
| DecimalPrecision.trunc(2.260, 2); | |
| DecimalPrecision.trunc(18.150, 2); | |
| DecimalPrecision.trunc(-2.260, 2); | |
| DecimalPrecision.trunc(-18.150, 2); | |
| DecimalPrecision.round(1262.48, -1); | |
| DecimalPrecision.round(1262.48, -2); | |
| }); | |
| suite.add("Solution 2 (Number.EPSILON)", function () { | |
| /** Solution 2 (Number.EPSILON) **/ | |
| DecimalPrecision2.round(0.5, 0); | |
| DecimalPrecision2.round(-0.5, 0); | |
| DecimalPrecision2.ceil(1e-8, 2); | |
| DecimalPrecision2.floor(1e-8, 2); | |
| DecimalPrecision2.round(5.12, 1); | |
| DecimalPrecision2.round(-5.12, 1); | |
| DecimalPrecision2.ceil(5.12, 1); | |
| DecimalPrecision2.ceil(-5.12, 1); | |
| DecimalPrecision2.floor(5.12, 1); | |
| DecimalPrecision2.floor(-5.12, 1); | |
| DecimalPrecision2.trunc(5.12, 1); | |
| DecimalPrecision2.trunc(-5.12, 1); | |
| DecimalPrecision2.round(1.005, 2); | |
| DecimalPrecision2.round(39.425, 2); | |
| DecimalPrecision2.round(-1.005, 2); | |
| DecimalPrecision2.round(-39.425, 2); | |
| DecimalPrecision2.ceil(9.130, 2); | |
| DecimalPrecision2.ceil(65.180, 2); | |
| DecimalPrecision2.ceil(-2.260, 2); | |
| DecimalPrecision2.ceil(-18.150, 2); | |
| DecimalPrecision2.floor(2.260, 2); | |
| DecimalPrecision2.floor(18.150, 2); | |
| DecimalPrecision2.floor(-9.130, 2); | |
| DecimalPrecision2.floor(-65.180, 2); | |
| DecimalPrecision2.trunc(2.260, 2); | |
| DecimalPrecision2.trunc(18.150, 2); | |
| DecimalPrecision2.trunc(-2.260, 2); | |
| DecimalPrecision2.trunc(-18.150, 2); | |
| DecimalPrecision2.round(1262.48, -1); | |
| DecimalPrecision2.round(1262.48, -2); | |
| }); | |
| suite.add("Solution 3 (Double rounding)", function () { | |
| /** Solution 3 (Double rounding) **/ | |
| DecimalPrecision3.round(0.5, 0); | |
| DecimalPrecision3.round(-0.5, 0); | |
| DecimalPrecision3.ceil(1e-8, 2); | |
| DecimalPrecision3.floor(1e-8, 2); | |
| DecimalPrecision3.round(5.12, 1); | |
| DecimalPrecision3.round(-5.12, 1); | |
| DecimalPrecision3.ceil(5.12, 1); | |
| DecimalPrecision3.ceil(-5.12, 1); | |
| DecimalPrecision3.floor(5.12, 1); | |
| DecimalPrecision3.floor(-5.12, 1); | |
| DecimalPrecision3.trunc(5.12, 1); | |
| DecimalPrecision3.trunc(-5.12, 1); | |
| DecimalPrecision3.round(1.005, 2); | |
| DecimalPrecision3.round(39.425, 2); | |
| DecimalPrecision3.round(-1.005, 2); | |
| DecimalPrecision3.round(-39.425, 2); | |
| DecimalPrecision3.ceil(9.130, 2); | |
| DecimalPrecision3.ceil(65.180, 2); | |
| DecimalPrecision3.ceil(-2.260, 2); | |
| DecimalPrecision3.ceil(-18.150, 2); | |
| DecimalPrecision3.floor(2.260, 2); | |
| DecimalPrecision3.floor(18.150, 2); | |
| DecimalPrecision3.floor(-9.130, 2); | |
| DecimalPrecision3.floor(-65.180, 2); | |
| DecimalPrecision3.trunc(2.260, 2); | |
| DecimalPrecision3.trunc(18.150, 2); | |
| DecimalPrecision3.trunc(-2.260, 2); | |
| DecimalPrecision3.trunc(-18.150, 2); | |
| DecimalPrecision3.round(1262.48, -1); | |
| DecimalPrecision3.round(1262.48, -2); | |
| }); | |
| suite.add("Solution 4 (Double rounding v2)", function () { | |
| /** Solution 4 (Double rounding v2) **/ | |
| DecimalPrecision4.round(0.5, 0); | |
| DecimalPrecision4.round(-0.5, 0); | |
| DecimalPrecision4.ceil(1e-8, 2); | |
| DecimalPrecision4.floor(1e-8, 2); | |
| DecimalPrecision4.round(5.12, 1); | |
| DecimalPrecision4.round(-5.12, 1); | |
| DecimalPrecision4.ceil(5.12, 1); | |
| DecimalPrecision4.ceil(-5.12, 1); | |
| DecimalPrecision4.floor(5.12, 1); | |
| DecimalPrecision4.floor(-5.12, 1); | |
| DecimalPrecision4.trunc(5.12, 1); | |
| DecimalPrecision4.trunc(-5.12, 1); | |
| DecimalPrecision4.round(1.005, 2); | |
| DecimalPrecision4.round(39.425, 2); | |
| DecimalPrecision4.round(-1.005, 2); | |
| DecimalPrecision4.round(-39.425, 2); | |
| DecimalPrecision4.ceil(9.130, 2); | |
| DecimalPrecision4.ceil(65.180, 2); | |
| DecimalPrecision4.ceil(-2.260, 2); | |
| DecimalPrecision4.ceil(-18.150, 2); | |
| DecimalPrecision4.floor(2.260, 2); | |
| DecimalPrecision4.floor(18.150, 2); | |
| DecimalPrecision4.floor(-9.130, 2); | |
| DecimalPrecision4.floor(-65.180, 2); | |
| DecimalPrecision4.trunc(2.260, 2); | |
| DecimalPrecision4.trunc(18.150, 2); | |
| DecimalPrecision4.trunc(-2.260, 2); | |
| DecimalPrecision4.trunc(-18.150, 2); | |
| DecimalPrecision4.round(1262.48, -1); | |
| DecimalPrecision4.round(1262.48, -2); | |
| }); | |
| suite.on("cycle", function (evt) { | |
| console.log(" - " + evt.target); | |
| }); | |
| suite.on("complete", function (evt) { | |
| console.log(new Array(30).join("-")); | |
| var results = evt.currentTarget.sort(function (a, b) { | |
| return b.hz - a.hz; | |
| }); | |
| results.forEach(function (item) { | |
| console.log((idx + 1) + ". " + item); | |
| }); | |
| }); | |
| console.log("Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2 #jsbench"); | |
| console.log(new Array(30).join("-")); | |
| suite.run(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment