Last active
November 11, 2020 09:11
-
-
Save wooandoo/682a0ae3a788672fd660194c65f5499e to your computer and use it in GitHub Desktop.
direct vs pipe #jsbench #jsperf (http://jsbench.github.io/#682a0ae3a788672fd660194c65f5499e) #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>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> |
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 () { | |
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