Created
July 26, 2017 19:49
-
-
Save EWhite613/458c639d6b581adde93cc64c340d1874 to your computer and use it in GitHub Desktop.
Fastest decimal point check (http://jsbench.github.io/#458c639d6b581adde93cc64c340d1874) #jsbench #jsperf
This file contains 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>Fastest decimal point check</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 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; | |
suite.add("+(Math.round(192.16826 + \"e+4\") + \"e-4\") === 192.16826", function () { | |
+(Math.round(192.16826 + "e+4") + "e-4") === 192.16826 | |
}); | |
suite.add("function decimalPlaces(num) {", function () { | |
function decimalPlaces(num) { | |
var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); | |
if (!match) { return 0; } | |
return Math.max( | |
0, | |
// Number of digits right of decimal point. | |
(match[1] ? match[1].length : 0) | |
// Adjust for scientific notation. | |
- (match[2] ? +match[2] : 0)); | |
} | |
decimalPlaces(192.16826) <= 4 | |
}); | |
suite.add("getPrecision = function(value) {", function () { | |
getPrecision = function(value) { | |
var v = value.valueOf(); | |
if (Math.floor(v) === v) return 0; | |
var str = value.toString(); | |
var ep = str.split("e-"); | |
if (ep.length > 1) { | |
var np = Number(ep[0]); | |
return np.getPrecision() + Number(ep[1]); | |
} | |
var dp = str.split("."); | |
if (dp.length > 1) { | |
return dp[1].length; | |
} | |
return 0; | |
} | |
getPrecision(192.16826) <= 4 | |
}); | |
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("Fastest decimal point check"); | |
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