Last active
January 19, 2021 18:54
-
-
Save DerekZiemba/87313123406b8ba610e86e4a79088f2c to your computer and use it in GitHub Desktop.
For-Each Compact Performance.jsbench (http://jsbench.github.io/#87313123406b8ba610e86e4a79088f2c) #jsbench #jsperf
This file contains 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>For-Each Compact 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> |
This file contains 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 () { | |
const testArray = [ | |
0, | |
"page-wrapper", | |
undefined, | |
"what", | |
null, | |
"document-page", | |
"", | |
1, | |
"test", | |
[], | |
{}, | |
['subarray'], | |
"random", | |
false, | |
"idk", | |
true, | |
['subarray', 'with', 'many', 'values'], | |
function dummyFuncNoArgs() {}, | |
function dummyfunc(with_some, args) {}, | |
"", | |
[], | |
" not empty ", | |
Symbol.for('last-element') | |
]; | |
const isArray = Array.isArray; | |
function compact_forI(aArray) { | |
const len = aArray.length; | |
if (len === 0) { return aArray; } | |
const result = new Array(len); | |
let count = 0; | |
for (let i = 0, val; val = aArray[i], i < len; ++i) if (val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true)) { | |
result[count++] = val; | |
} | |
result.length = count; | |
return result; | |
} | |
function compact_forOf(aArray) { | |
const result = new Array(aArray.length); | |
let count = 0; | |
for (let val of aArray) if (val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true)) { | |
result[count++] = val; | |
} | |
result.length = count; | |
return result; | |
} | |
function compact_forEachPrealloc(aArray) { | |
const result = new Array(aArray.length); | |
let count = 0; | |
aArray.forEach(val => { | |
if (val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true)) { result[count++] = val; } | |
}); | |
result.length = count; | |
return result; | |
} | |
function compact_forEachPush(aArray) { | |
const result = []; | |
aArray.forEach(val => { | |
if (val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true)) { result.push(val); } | |
}); | |
return result; | |
} | |
function compact_filter(aArray) { | |
return aArray.filter(val => val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true)); | |
} | |
const isSafeInteger = Number.isSafeInteger; | |
const hasLength = val => val != null && isSafeInteger(val.length) && val.length > 0 | |
function compact_hasLength(aArray) { | |
return aArray.filter(hasLength); | |
} | |
}; | |
suite.add("testArray.filter(val => val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true));", function () { | |
testArray.filter(val => val != null && (typeof val === 'string' || isArray(val) ? val.length > 0 : true)); | |
}); | |
suite.add("testArray.filter(val => val != null && val.length);", function () { | |
testArray.filter(val => val != null && val.length); | |
}); | |
suite.add("compact_forI", function () { | |
//compact_forI | |
compact_forI(testArray); | |
}); | |
suite.add("compact_forOf", function () { | |
//compact_forOf | |
compact_forOf(testArray); | |
}); | |
suite.add("compact_forEachPrealloc", function () { | |
//compact_forEachPrealloc | |
compact_forEachPrealloc(testArray); | |
}); | |
suite.add("compact_forEachPush", function () { | |
//compact_forEachPush | |
compact_forEachPush(testArray); | |
}); | |
suite.add("compact_filter", function () { | |
//compact_filter | |
compact_filter(testArray); | |
}); | |
suite.add("compact_hasLength", function () { | |
//compact_hasLength | |
compact_hasLength(testArray); | |
}); | |
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("For-Each Compact 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