-
-
Save amrali-eg/fe83aba8b0a3101c450e815c79be3549 to your computer and use it in GitHub Desktop.
Untitled benchmark (http://jsbench.github.io/#fe83aba8b0a3101c450e815c79be3549) #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>Untitled benchmark</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); | |
| }; | |
| } | |
| return { | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| num = (num + 'e').split('e'); | |
| num = Math.sign(+num[0]) * Math.round(Math.abs(+num[0]) + 'e' + (+num[1] + decimalPlaces)); | |
| num = (num + 'e').split('e'); | |
| return +(num[0] + 'e' + (+num[1] - decimalPlaces)); | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| num = (num + 'e').split('e'); | |
| num = Math.ceil(num[0] + 'e' + (+num[1] + decimalPlaces)); | |
| num = (num + 'e').split('e'); | |
| return +(num[0] + 'e' + (+num[1] - decimalPlaces)); | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| num = (num + 'e').split('e'); | |
| num = Math.floor(num[0] + 'e' + (+num[1] + decimalPlaces)); | |
| num = (num + 'e').split('e'); | |
| return +(num[0] + 'e' + (+num[1] - decimalPlaces)); | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| num = (num + 'e').split('e'); | |
| num = Math.trunc(num[0] + 'e' + (+num[1] + decimalPlaces)); | |
| num = (num + 'e').split('e'); | |
| return +(num[0] + 'e' + (+num[1] - 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); | |
| }; | |
| } | |
| return { | |
| isRound: function(num, decimalPlaces) { | |
| //return this.round(num, decimalPlaces) === num; | |
| return decimalPlaces >= 0 && | |
| +num.toFixed(decimalPlaces) === num; | |
| }, | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| var p = Math.pow(10, decimalPlaces); | |
| var e = Number.EPSILON * num * p; | |
| return Math.round((num * p) + e) / p; | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| if (this.isRound(num, decimalPlaces)) | |
| return num; | |
| var p = Math.pow(10, decimalPlaces); | |
| return Math.ceil(num * p) / p; | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| if (this.isRound(num, decimalPlaces)) | |
| return num; | |
| var p = Math.pow(10, decimalPlaces); | |
| return Math.floor(num * p) / p; | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| if (this.isRound(num, decimalPlaces)) | |
| return num; | |
| var p = Math.pow(10, decimalPlaces); | |
| return Math.trunc(num * p) / p; | |
| } | |
| }; | |
| })(); | |
| // Solution 3 | |
| var DecimalPrecision3 = (function() { | |
| if (Math.trunc === undefined) { | |
| Math.trunc = function(v) { | |
| return v < 0 ? Math.ceil(v) : Math.floor(v); | |
| }; | |
| } | |
| return { | |
| // Decimal round (half away from zero) | |
| round: function(num, decimalPlaces) { | |
| var d = decimalPlaces || 0; | |
| var m = Math.pow(10, d); | |
| var n = +(d ? num * m : num).toFixed(8); | |
| var i = Math.floor(n), f = n - i; | |
| var e = 1e-8; // Allow for rounding errors in f | |
| var r = (f > 0.5 - e && f < 0.5 + e) ? | |
| ((n < 0) ? i : i + 1) : | |
| Math.round(n); | |
| return d ? r / m : r; | |
| }, | |
| // Decimal ceil | |
| ceil: function(num, decimalPlaces) { | |
| var d = decimalPlaces || 0; | |
| var m = Math.pow(10, d); | |
| var n = +(d ? num * m : num).toFixed(8); | |
| var i = Math.ceil(n); | |
| return d ? i / m : i; | |
| }, | |
| // Decimal floor | |
| floor: function(num, decimalPlaces) { | |
| var d = decimalPlaces || 0; | |
| var m = Math.pow(10, d); | |
| var n = +(d ? num * m : num).toFixed(8); | |
| var i = Math.floor(n); | |
| return d ? i / m : i; | |
| }, | |
| // Decimal trunc | |
| trunc: function(num, decimalPlaces) { | |
| var d = decimalPlaces || 0; | |
| var m = Math.pow(10, d); | |
| var n = +(d ? num * m : num).toFixed(8); | |
| var i = Math.trunc(n); | |
| return d ? i / m : i; | |
| } | |
| }; | |
| })(); | |
| }; | |
| 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("Untitled benchmark"); | |
| 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