Created
August 25, 2020 21:29
-
-
Save amrali-eg/a28de0dcb524c8c2988a27de465b8b0a to your computer and use it in GitHub Desktop.
Untitled benchmark (http://jsbench.github.io/#a28de0dcb524c8c2988a27de465b8b0a) #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 DecimalPrecision1 = (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); | |
}; | |
} | |
// Decimal round (half away from zero) | |
this.round = function(num, digits) { | |
num = (num + 'e').split('e'); | |
num = Math.sign(+num[0]) * Math.round(Math.abs(+num[0]) + 'e' + (+num[1] + digits)); | |
num = (num + 'e').split('e'); | |
return +(num[0] + 'e' + (+num[1] - digits)); | |
}; | |
// Decimal ceil | |
this.ceil = function(num, digits) { | |
num = (num + 'e').split('e'); | |
num = Math.ceil(num[0] + 'e' + (+num[1] + digits)); | |
num = (num + 'e').split('e'); | |
return +(num[0] + 'e' + (+num[1] - digits)); | |
}; | |
// Decimal floor | |
this.floor = function(num, digits) { | |
num = (num + 'e').split('e'); | |
num = Math.floor(num[0] + 'e' + (+num[1] + digits)); | |
num = (num + 'e').split('e'); | |
return +(num[0] + 'e' + (+num[1] - digits)); | |
}; | |
// Decimal trunc | |
this.trunc = function(num, digits) { | |
num = (num + 'e').split('e'); | |
num = Math.trunc(num[0] + 'e' + (+num[1] + digits)); | |
num = (num + 'e').split('e'); | |
return +(num[0] + 'e' + (+num[1] - digits)); | |
}; | |
return this; | |
})(); | |
// 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); | |
}; | |
} | |
this.isRound = function(num, digits) { | |
//return this.round(num, digits) === num; | |
return +num.toFixed(digits) === num; | |
}; | |
// Decimal round (half away from zero) | |
this.round = function(num, digits) { | |
var e = 0.51 * Number.EPSILON * num; | |
var p = Math.pow(10, digits); | |
return Math.round((num + e) * p) / p; | |
}; | |
// Decimal ceil | |
this.ceil = function(num, digits) { | |
if (this.isRound(num, digits)) | |
return num; | |
var p = Math.pow(10, digits); | |
return Math.ceil(num * p) / p; | |
}; | |
// Decimal floor | |
this.floor = function(num, digits) { | |
if (this.isRound(num, digits)) | |
return num; | |
var p = Math.pow(10, digits); | |
return Math.floor(num * p) / p; | |
}; | |
// Decimal trunc | |
this.trunc = function(num, digits) { | |
if (this.isRound(num, digits)) | |
return num; | |
var p = Math.pow(10, digits); | |
return Math.trunc(num * p) / p; | |
}; | |
return this; | |
})(); | |
}; | |
suite.add("Solution 1 (String) **/", function () { | |
/** Solution 1 (String) **/ | |
DecimalPrecision1.round(0.5, 0); | |
DecimalPrecision1.round(-0.5, 0); | |
DecimalPrecision1.ceil(1e-8, 2); | |
DecimalPrecision1.floor(1e-8, 2); | |
DecimalPrecision1.round(5.12, 1); | |
DecimalPrecision1.round(-5.12, 1); | |
DecimalPrecision1.ceil(5.12, 1); | |
DecimalPrecision1.ceil(-5.12, 1); | |
DecimalPrecision1.floor(5.12, 1); | |
DecimalPrecision1.floor(-5.12, 1); | |
DecimalPrecision1.trunc(5.12, 1); | |
DecimalPrecision1.trunc(-5.12, 1); | |
DecimalPrecision1.round(1.005, 2); | |
DecimalPrecision1.round(39.425, 2); | |
DecimalPrecision1.round(-1.005, 2); | |
DecimalPrecision1.round(-39.425, 2); | |
DecimalPrecision1.ceil(9.130, 2); | |
DecimalPrecision1.ceil(65.180, 2); | |
DecimalPrecision1.ceil(-2.260, 2); | |
DecimalPrecision1.ceil(-18.150, 2); | |
DecimalPrecision1.floor(2.260, 2); | |
DecimalPrecision1.floor(18.150, 2); | |
DecimalPrecision1.floor(-9.130, 2); | |
DecimalPrecision1.floor(-65.180, 2); | |
DecimalPrecision1.trunc(2.260, 2); | |
DecimalPrecision1.trunc(18.150, 2); | |
DecimalPrecision1.trunc(-2.260, 2); | |
DecimalPrecision1.trunc(-18.150, 2); | |
DecimalPrecision1.round(1262.48, -1); | |
DecimalPrecision1.round(1262.48, -2); | |
}); | |
suite.add("Solution 2 (Arithmetic) **/", function () { | |
/** Solution 2 (Arithmetic) **/ | |
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.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