Last active
January 20, 2017 15:12
-
-
Save Yaffle/7a271ffbf88a5d4b0f5b42b13b89144e to your computer and use it in GitHub Desktop.
Math.cbrt #jsbench #jsperf (http://jsbench.github.io/#7a271ffbf88a5d4b0f5b42b13b89144e) #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>Math.cbrt #jsbench #jsperf</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 () { | |
var i = 0; | |
var s = 1024 * 16; | |
var x = new Array(s); | |
for (var j = 0; j < s; j += 1) { | |
x[j] = (j / s + 1) * Math.pow(2, j % 1024); | |
} | |
var f1 = function (x) { | |
x = +x; | |
return Math.cbrt(x); | |
}; | |
var f2 = function (x) { | |
x = +x; | |
var y = Math.pow(x, 1 / 3); | |
return ((x / (y * y)) + (2 * y)) / 3; | |
}; | |
var f3 = function (x) { | |
x = +x; | |
var y = Math.exp(Math.log(x) / 3); | |
return ((x / (y * y)) + (2 * y)) / 3; | |
}; | |
}; | |
suite.add("if (f1(x[i]) === 0) throw new Error(x[i]); i += 1; if (i > s) i = 0;", function () { | |
if (f1(x[i]) === 0) throw new Error(x[i]); i += 1; if (i > s) i = 0; | |
}); | |
suite.add("if (f2(x[i]) === 0) throw new Error(x[i]); i += 1; if (i > s) i = 0;", function () { | |
if (f2(x[i]) === 0) throw new Error(x[i]); i += 1; if (i > s) i = 0; | |
}); | |
suite.add("if (f3(x[i]) === 0) throw new Error(x[i]); i += 1; if (i > s) i = 0;", function () { | |
if (f3(x[i]) === 0) throw new Error(x[i]); i += 1; if (i > s) i = 0; | |
}); | |
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("Math.cbrt #jsbench #jsperf"); | |
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