Skip to content

Instantly share code, notes, and snippets.

@DerekZiemba
Last active April 15, 2020 21:55
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/7e604d68c13fb1b41e441a963cb9b764 to your computer and use it in GitHub Desktop.
Save DerekZiemba/7e604d68c13fb1b41e441a963cb9b764 to your computer and use it in GitHub Desktop.
Fastest way to filter/compact array (https://jsbench.github.io/#7e604d68c13fb1b41e441a963cb9b764) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Fastest way to filter/compact 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 () {
const words = [null, 'This', 'is', 'an', 1, 'array', false, 'of', 'words', 2, '', 'with', 0, 'some', '', '', 'odd', undefined, '', 'spacing', 'to', 0, 'test', 'filter'];
const hasValue = (x) => x != null && x != '';
Array.prototype.compact = function(){
return this.filter(hasValue);
};
Array.prototype.compact2 = function(){
var tmp = [];
for (var len = this.length, i = 0, val; i < len; ++i) {
val = this[i];
if (val != null && val != '') { tmp.push(val); }
}
return tmp;
};
Array.prototype.compactPresize = function(){
var len = this.length, tmp = Array(len), count = 0, i = 0, val;
for (; i < len; ++i) {
val = this[i];
if (val != null && val != '') {
tmp[count++] = val;
}
}
tmp.length = count;
return tmp;
};
Array.prototype.compactPresizeCheckType = function(){
var len = this.length, tmp = Array(len), count = 0, i = 0, val;
for (; i < len; ++i) {
val = this[i];
if ((typeof val === 'string' || Array.isArray(val)) ? val.length > 0 : val != null) {
tmp[count++] = val;
}
}
tmp.length = count;
return tmp;
};
var isArray = Array.isArray;
Array.prototype.compactStrictPresizeCheckType = function(){
var len = this.length, tmp = Array(len), count = 0, i = 0, val;
for (; i < len; ++i) {
val = this[i];
if (val !== undefined && val !== null && ((typeof val === 'string' || isArray(val)) ? val.length > 0 : true)) {
tmp[count++] = val;
}
}
tmp.length = count;
return tmp;
};
};
suite.add("Normal", function () {
// Normal
return words.filter((x) => x != null && x != '')
});
suite.add("return words.compact();", function () {
return words.compact();
});
suite.add("return words.compact2()", function () {
return words.compact2()
});
suite.add("return words.compactPresize()", function () {
return words.compactPresize()
});
suite.add("return words.compactPresizeCheckType()", function () {
return words.compactPresizeCheckType()
});
suite.add("return words.compactStrictPresizeCheckType()", function () {
return words.compactStrictPresizeCheckType()
});
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 filter/compact 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