-
-
Save DerekZiemba/e86c06909d7eedd18f8b9bac101e9d6d to your computer and use it in GitHub Desktop.
Loop Performance 2 .jsbench (https://jsbench.github.io/#e86c06909d7eedd18f8b9bac101e9d6d) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Loop Performance 2 .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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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 OwnForEach(arr, cb) { | |
for (var len = arr.length, idx = 0; idx < len; idx++) { cb(arr[idx], idx, arr); } | |
} | |
var staticForEach = Function.prototype.call.bind(Array.prototype.forEach); | |
function shuffleArrayOfArrays(array) { | |
var end = array.length; | |
do { | |
for (var i = end - 1; i > 0; i--) { | |
var j = Math.floor(Math.random() * (i + 1)); | |
var temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
var keepGoing = array[0].length === 0 || array[end-1].length === 0 || array[0].length === array[end-1].length; | |
for (var i = 0, len = end - 1; i < len && keepGoing===false; i++) { | |
keepGoing = array[i].length === array[i+1].length; | |
} | |
} while (keepGoing); | |
return array; | |
} | |
var srcTestingData = ((generateTestData)=>{ | |
var data = window.testSourceData; | |
if (!data) { | |
data = generateTestData(); | |
window.testSourceData = data; | |
} | |
return data; | |
})(()=>{ | |
const testArrays = []; | |
for(var multis = [1, 3, 7, 13, 23, 31, 43, 67, 89, 269, 569], spacers = [0,1,2,3,5,7], iM=0; iM < multis.length; iM++) { | |
for(var iS=0; iS < spacers.length; iS++) { | |
let testArray = [] | |
for(var count = multis[iM]*spacers[iS]; count > 0; count--) { | |
var rando = (((1.2*Math.random())-Math.random()) * Math.pow(10,5))|0; | |
testArray.push(rando); | |
} | |
testArrays.push(testArray); | |
} | |
} | |
shuffleArrayOfArrays(testArrays); | |
var correctSums = testArrays.map(testArray => { | |
var sum = 0; | |
for(var i =0; i< testArray.length; i++) { | |
sum = sum + testArray[i]; | |
} | |
return sum; | |
}); | |
Object.defineProperty(testArrays, 'correctSums', { enumerable: false, writable: false, value: correctSums }); | |
return testArrays; | |
}); | |
}; | |
suite.add("Native forEach Arrow", function () { | |
//Native forEach Arrow | |
srcTestingData.forEach((testArray, testIdx, data) => { | |
let sum = 0; | |
testArray.forEach(value=> sum+=value); | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
}); | |
}); | |
suite.add("Native forEach func", function () { | |
//Native forEach func | |
srcTestingData.forEach(function (testArray, testIdx) { | |
let sum = 0; | |
testArray.forEach(function (value) { | |
sum+=value; | |
}); | |
if (this.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
}, srcTestingData); | |
}); | |
suite.add("Own ForEach", function () { | |
//Own ForEach | |
let forEach = OwnForEach; | |
forEach(srcTestingData, (testArray, testIdx, data) => { | |
let sum = 0; | |
forEach(testArray, value=> sum+=value); | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
}); | |
}); | |
suite.add("for...of", function () { | |
//for...of | |
const data = srcTestingData; | |
let testIdx = 0; | |
for(let testArray of data) { | |
let sum = 0; | |
for(let value of testArray) { | |
sum+=value; | |
} | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
testIdx++; | |
} | |
}); | |
suite.add("for loop", function () { | |
//for loop | |
const data = srcTestingData; | |
for(let testIdx = 0; testIdx < data.length; testIdx++) { | |
const testArray = data[testIdx]; | |
let sum = 0; | |
for(let i = 0; i < testArray.length; i++) { | |
let value = testArray[i]; | |
sum+=value; | |
} | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
} | |
}); | |
suite.add("for loop (optimized)", function () { | |
//for loop (optimized) | |
for(let data = srcTestingData, testLen = data.length, testIdx = 0, sum, testArray, i, len; testIdx < testLen; testIdx++) { | |
for(sum=0, len = (testArray=data[testIdx]).length, i = 0; i < len; i++) { | |
sum+=testArray[i]; | |
} | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
} | |
}); | |
suite.add("Array.prototype.forEach.call", function () { | |
//Array.prototype.forEach.call | |
const forEach = Array.prototype.forEach; | |
forEach.call(srcTestingData, (testArray, testIdx, data) => { | |
let sum = 0; | |
forEach.call(testArray, value=> sum+=value); | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
}); | |
}); | |
suite.add("call.bind(Array.prototype.forEach)", function () { | |
//call.bind(Array.prototype.forEach) | |
const forEach = Function.prototype.call.bind(Array.prototype.forEach); | |
forEach(srcTestingData, (testArray, testIdx, data) => { | |
let sum = 0; | |
forEach(testArray, value=> sum+=value); | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
}); | |
}); | |
suite.add("static call.bind", function () { | |
//static call.bind | |
const forEach = staticForEach; | |
forEach(srcTestingData, (testArray, testIdx, data) => { | |
let sum = 0; | |
forEach(testArray, value=> sum+=value); | |
if (data.correctSums[testIdx] !== sum) { throw Error(`${testIdx}\n${sum}\n${data.correctSums[testIdx]}`); } | |
}); | |
}); | |
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 2 .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