Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Last active October 5, 2019 04:32
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 jridgewell/be658e0459613a031c01c1df83aca059 to your computer and use it in GitHub Desktop.
Save jridgewell/be658e0459613a031c01c1df83aca059 to your computer and use it in GitHub Desktop.
Summing rest vs var args (https://jsbench.github.io/#be658e0459613a031c01c1df83aca059) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Summing rest vs var args</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 rest(...args) {
let sum = 0;
for (let i = 0; i < args.length; i++) sum += args[i];
return sum;
}
function args() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) sum += arguments[i];
return sum;
}
function unusedRest(...args) {
let sum = 0;
for (let i = 0; i < arguments.length; i++) sum += arguments[i];
return sum;
}
};
suite.add("Rest args, empty", function () {
// Rest args, empty
rest()
});
suite.add("Arguments args, empty", function () {
// Arguments args, empty
args()
});
suite.add("Rest args, unused, empty", function () {
// Rest args, unused, empty
unusedRest()
});
suite.add("Rest args, small", function () {
// Rest args, small
rest(1, 2, 3)
});
suite.add("Arguments args, small", function () {
// Arguments args, small
args(1, 2, 3)
});
suite.add("Rest args, unused, small", function () {
// Rest args, unused, small
unusedRest(1, 2, 3)
});
suite.add("Rest args, medium", function () {
// Rest args, medium
rest(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
});
suite.add("Arguments args, medium", function () {
// Arguments args, medium
args(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
});
suite.add("Rest args, unused, medium", function () {
// Rest args, unused, medium
unusedRest(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
});
suite.add("Rest args, large", function () {
// Rest args, large
rest(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)
});
suite.add("Arguments args, large", function () {
// Arguments args, large
args(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)
});
suite.add("Rest args, unused, large", function () {
// Rest args, unused, large
unusedRest(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)
});
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("Summing rest vs var args");
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