Skip to content

Instantly share code, notes, and snippets.

@DerekZiemba
Created March 9, 2017 21:40
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/785b8f08d2eaef25b16831460472ea94 to your computer and use it in GitHub Desktop.
Save DerekZiemba/785b8f08d2eaef25b16831460472ea94 to your computer and use it in GitHub Desktop.
Loop Performance2 Different Order .jsbench (http://jsbench.github.io/#785b8f08d2eaef25b16831460472ea94) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Loop Performance .jsbench</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 () {
var s = document.createElement('script');
s.src = 'https://code.jquery.com/jquery-3.1.1.slim.min.js';
document.head.appendChild(s);
var sumValue = 0;
var sumIdx = 0;
var sumCalls = 0;
function action(value, idx) {
sumValue += value;
sumIdx += idx;
sumCalls++;
}
function ForEach(arr, cb) {
if (arr && arr.length) {
for (var idx = 0, len = arr.length; idx < len; idx++) { cb(arr[idx], idx); }
}
}
Array.prototype.delegateForEach = function (cb) { ForEach(this, cb); }
Array.prototype.ForEach = function (cb) { for (var idx = 0, len = this.length; idx < len; idx++) { cb(this[idx], idx); } }
function initArray(arr) {
for (var idx = 0, len = arr.length; idx < len; idx++) {
arr[idx] = (Math.random() / (Math.random() * Math.random()) * 1.2);// | 0;
}
return arr;
}
var arrRandom1000 = initArray(new Array(1000));
};
Benchmark.prototype.teardown = function () {
sumValue = 0;
sumIdx = 0;
sumCalls = 0;
};
suite.add("Native.forEach", function () {
//Native.forEach
arrRandom1000.forEach(action);
});
suite.add("Native.Proto.forEach", function () {
//Native.Proto.forEach
Array.prototype.forEach.call(arrRandom1000, action);
});
suite.add("Native.Proto.forEach.Anon", function () {
//Native.Proto.forEach.Anon
arrRandom1000.forEach(function (value, idx) {
sumValue += value;
sumIdx += idx;
sumCalls++;
});
});
suite.add("std.forLoop.wCall", function () {
//std.forLoop.wCall
for (var idx = 0, len = arrRandom1000.length; idx < len; idx++) {
action(arrRandom1000[idx], idx);
}
});
suite.add("std.forLoop.wBody", function () {
//std.forLoop.wBody
for (var idx = 0, len = arrRandom1000.length; idx < len; idx++) {
sumValue += arrRandom1000[idx];
sumIdx += idx;
sumCalls++;
}
});
suite.add("jQuery.each", function () {
//jQuery.each
$.each(arrRandom1000, action);
});
suite.add("jQuery.proto.each.call", function () {
//jQuery.proto.each.call
$.prototype.each.call(arrRandom1000, action);
});
suite.add("jQuery.each.Anon", function () {
//jQuery.each.Anon
$.each(arrRandom1000, function (idx, value) {
sumValue += value;
sumIdx += idx;
sumCalls++;
});
});
suite.add("My.ForEach", function () {
//My.ForEach
ForEach(arrRandom1000, action);
});
suite.add("My.Proto.ForEach", function () {
//My.Proto.ForEach
arrRandom1000.ForEach(action);
});
suite.add("My.Proto.ForEach.Call", function () {
//My.Proto.ForEach.Call
Array.prototype.ForEach.call(arrRandom1000, action);
});
suite.add("My.Proto.DelegateForEach", function () {
//My.Proto.DelegateForEach
arrRandom1000.delegateForEach(action);
});
suite.add("My.ForEach.Anon", function () {
//My.ForEach.Anon
ForEach(arrRandom1000, function (value, idx) {
sumValue += value;
sumIdx += idx;
sumCalls++;
});
});
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("Loop Performance .jsbench");
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