Skip to content

Instantly share code, notes, and snippets.

@wooandoo
Last active November 11, 2020 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wooandoo/682a0ae3a788672fd660194c65f5499e to your computer and use it in GitHub Desktop.
Save wooandoo/682a0ae3a788672fd660194c65f5499e to your computer and use it in GitHub Desktop.
direct vs pipe #jsbench #jsperf (http://jsbench.github.io/#682a0ae3a788672fd660194c65f5499e) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>direct vs pipe #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>
"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 () {
console.clear()
const increment = fn => () => fn() +1
const add = value => value => fn => () => fn() + value
const double = fn => () => 2 * fn()
const get = value => () => value
const decorate = (...decorators) => fn => decorators.reduce(
(decorated_fn, decorator) => decorator(decorated_fn),
fn
)
const decorate2 = (...decorators) => fn => {
let decorated_fn = fn
for (const decorator of decorators) {
decorated_fn = decorator(decorated_fn)
}
return decorated_fn
}
const direct_demo = x => 2 * ((7 +1) +5)
const direct_compose_demo = x => double(add(5)(increment(get(7))))
const pipe_demo = decorate(increment, add(5), double)(get(7))
const pipe2_demo = decorate2(increment, add(5), double)(get(7))
};
suite.add("direct_demo()", function () {
direct_demo()
});
suite.add("pipe_demo()", function () {
pipe_demo()
});
suite.add("pipe2_demo()", function () {
pipe2_demo()
});
suite.add("direct_compose_demo()", function () {
direct_compose_demo()
});
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("direct vs pipe #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