Skip to content

Instantly share code, notes, and snippets.

@DerekZiemba
Last active February 4, 2020 01:39
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 DerekZiemba/23acc7fe843ce3d2cf009ed0cec10428 to your computer and use it in GitHub Desktop.
Save DerekZiemba/23acc7fe843ce3d2cf009ed0cec10428 to your computer and use it in GitHub Desktop.
Fastest way to convert arguments to array (https://jsbench.github.io/#23acc7fe843ce3d2cf009ed0cec10428) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Fastest way to convert arguments to array</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 makeArgs(f) {
let result = f({}, 1, 2.0, 3.0, 4, 5, 6.0);
if (result !== 21.0) { throw new Error("Bad Implementation: " + result); }
return result;
}
function action(one, two, three, four, five, six){
return one + two + three + four + five + six;
}
};
suite.add("Baseline", function () {
//Baseline
makeArgs((obj, x1, x2, x3, x4, x5, x6, x7, x8, x9) => action(x1, x2, x3, x4, x5, x6, x7, x8, x9));
});
suite.add("Arguments Direct", function () {
//Arguments Direct
makeArgs(function() { return action(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]); });
});
suite.add("Arguments Direct Call", function () {
//Arguments Direct Call
makeArgs(function() { return action.call(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]); });
});
suite.add("Arguments Shotgun", function () {
//Arguments Shotgun
makeArgs(function() { return action(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9]); });
});
suite.add("Array Slice", function () {
//Array Slice
makeArgs(function() { return action.apply(null, Array.prototype.slice.call(arguments, 1)); });
});
suite.add("For Loop", function () {
//For Loop
makeArgs(function() {
var len = arguments.length, args = new Array(len - 1);
for(let i = 1; i < len; i++){ args[i-1] = arguments[i]; }
return action.apply(null, args);
});
});
suite.add("For Loop Spread", function () {
//For Loop Spread
makeArgs(function() {
var len = arguments.length, args = new Array(len - 1);
for(let i = 1; i < len; i++){ args[i-1] = arguments[i]; }
return action(...args);
});
});
suite.add("Spread Baseline", function () {
//Spread Baseline
makeArgs((obj, ...args) => action(...args));
});
suite.add("Apply Overhead", function () {
//Apply Overhead
makeArgs((obj, ...args) => action.apply(obj, args));
});
suite.add("Spread Overhead", function () {
//Spread Overhead
makeArgs(function() { return action.call(...arguments)} );
});
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("Fastest way to convert arguments to array");
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