Skip to content

Instantly share code, notes, and snippets.

@developerdizzle
Last active April 20, 2017 18:21
Show Gist options
  • Save developerdizzle/5115034c61a45940f7d022b9d94a5811 to your computer and use it in GitHub Desktop.
Save developerdizzle/5115034c61a45940f7d022b9d94a5811 to your computer and use it in GitHub Desktop.
const predicate inside vs outside method scope (http://jsbench.github.io/#5115034c61a45940f7d022b9d94a5811) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>const predicate inside vs outside method scope</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;
suite.add("const isFive = s => s === 5;", function () {
const isFive = s => s === 5;
const getFives = items => {
return items.filter(isFive);
}
const fives = getFives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
suite.add("const getFives = items => {", function () {
const getFives = items => {
const isFive = s => s === 5;
return items.filter(isFive);
}
const fives = getFives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
suite.add("const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];", function () {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const isFive = s => s === 5;
const getFives = () => {
return items.filter(isFive);
}
const fives = getFives();
});
suite.add("const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];", function () {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const getFives = () => {
const isFive = s => s === 5;
return items.filter(isFive);
}
const fives = getFives();
});
suite.add("const getFives = () => {", function () {
const getFives = () => {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const isFive = s => s === 5;
return items.filter(isFive);
}
const fives = getFives();
});
suite.add("const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];", function () {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const getFives = items => {
const isFive = s => s === 5;
return items.filter(isFive);
}
const fives = getFives(items);
});
suite.add("const getFives = items => {", function () {
const getFives = items => {
return items.filter(s => s === 5);
}
const fives = getFives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
suite.add("const getFives = () => {", function () {
const getFives = () => {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].filter(s => s === 5);
}
const fives = getFives();
});
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("const predicate inside vs outside method scope");
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