Created
February 21, 2020 16:19
-
-
Save wooandoo/3955bbc7334a32f321dce1641fe632ec to your computer and use it in GitHub Desktop.
compose filter vs wrapped filter vs filter in one step (http://jsbench.github.io/#3955bbc7334a32f321dce1641fe632ec) #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>compose filter vs wrapped filter vs filter in one step</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 () { | |
function dynamicallyLoadScript(url) { | |
var script = document.createElement("script"); // create a script DOM node | |
script.src = url; // set its src to the provided URL | |
document.head.appendChild(script); // add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead) | |
} | |
dynamicallyLoadScript("https://cdn.jsdelivr.net/npm/[email protected]/dist/ramda.min.js") | |
const { filter, map, compose } = R | |
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
const is_event = value => value % 2 === 0 | |
const is_less_than = max_value => value => value < max_value | |
const is_less_than_6 = is_less_than(6) | |
const filtered_values = compose( | |
filter(is_event), | |
filter(is_less_than(6)) | |
) | |
}; | |
suite.add("filter(value => is_event(value) && is_less_than_6(value), array)", function () { | |
filter(value => is_event(value) && is_less_than_6(value), array) | |
}); | |
suite.add("filter(", function () { | |
filter( | |
is_event, | |
filter(is_less_than_6, array) | |
) | |
}); | |
suite.add("filtered_values(array)", function () { | |
filtered_values(array) | |
}); | |
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("compose filter vs wrapped filter vs filter in one step"); | |
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