Skip to content

Instantly share code, notes, and snippets.

@wooandoo
Last active December 4, 2020 08:14
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/6d9fb88c091a5be96b554f041270b0b2 to your computer and use it in GitHub Desktop.
Save wooandoo/6d9fb88c091a5be96b554f041270b0b2 to your computer and use it in GitHub Desktop.
functor vs local variable #jsbench #jsperf (https://jsbench.github.io/#6d9fb88c091a5be96b554f041270b0b2) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>functor vs local variable #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 () {
const functor = (value) => {
return {
map: (fn) => functor(fn(value)),
value: () => value
}
}
class Functor {
constructor(value) {
this.value
}
map(fn) {
return new Functor(fn(this.value))
}
value() {
return this.value
}
}
const fn1 = (value) => value +1
const fn2 = (value) => value *2
};
suite.add("const temp_value = fn1(10)", function () {
const temp_value = fn1(10)
fn2(temp_value)
});
suite.add("functor(10)", function () {
functor(10)
.map(fn1)
.map(fn2)
.value()
});
suite.add("new Functor(10)", function () {
new Functor(10)
.map(fn1)
.map(fn2)
.value()
});
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("functor vs local variable #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