Skip to content

Instantly share code, notes, and snippets.

@wooandoo
Created September 23, 2020 06:30
Show Gist options
  • Select an option

  • Save wooandoo/2f7a71dd36b5c85ce64cf17c71568f07 to your computer and use it in GitHub Desktop.

Select an option

Save wooandoo/2f7a71dd36b5c85ce64cf17c71568f07 to your computer and use it in GitHub Desktop.
scan values(by_id) vs ids.map() (http://jsbench.github.io/#2f7a71dd36b5c85ce64cf17c71568f07) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>scan values(by_id) vs ids.map()</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 () {
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 data = R.range(0, 100).reduce(
(repo, id) => {
repo.by_id[id] = {
id,
name: `ITEM ${id}`
}
repo.ids.push(id)
return repo
},
{ by_id: {}, ids: [] }
)
};
suite.add("data.ids.reduce(", function () {
data.ids.reduce(
(result, id) => {
const item = data.by_id[id]
return result + item.id
},
0
)
});
suite.add("R.values(data.by_id).reduce(", function () {
R.values(data.by_id).reduce(
(result, item) => {
return result + item.id
},
0
)
});
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("scan values(by_id) vs ids.map()");
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