Created
February 25, 2017 00:16
-
-
Save dmoralesm/723899c9fd4dba58575b03f2177c172b to your computer and use it in GitHub Desktop.
Array.unique implementation (http://jsbench.github.io/#723899c9fd4dba58575b03f2177c172b) #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>Array.unique implementation</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 () { | |
Array.prototype.random = function(n) { | |
var arr = [] | |
for(var i = 0; i < n; i++) { | |
arr.push(parseInt(Math.random() * 1000)) | |
} | |
return arr | |
} | |
var myArr = [].random(100000) | |
}; | |
suite.add("Array.prototype.unique = function() {", function () { | |
Array.prototype.unique = function() { | |
var uniqueArr = [this[0]] | |
for(var i = 1; i < this.length - 1; i++) { | |
if(uniqueArr.indexOf(this[i]) == -1) { | |
uniqueArr.push(this[i]) | |
} | |
} | |
return uniqueArr | |
} | |
console.log(myArr.unique()) | |
}); | |
suite.add("Array.prototype.unique = function() {", function () { | |
Array.prototype.unique = function() { | |
var obj = {}, unique = [] | |
for(var i = 0; i < this.length - 1; i++) { | |
if(!obj[i]) { | |
obj[this[i]] = true | |
unique.push(this[i]) | |
} | |
} | |
return unique | |
} | |
console.log(myArr.unique()) | |
}); | |
suite.add("console.log([...new Set(myArr)]) // ES6", function () { | |
console.log([...new Set(myArr)]) // ES6 | |
}); | |
suite.add("Array.prototype.unique = function() {", function () { | |
Array.prototype.unique = function() { | |
return this.filter(function(value, index, origArr) { | |
return origArr.indexOf(value) === index | |
}) | |
} | |
console.log(myArr.unique()) | |
}); | |
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("Array.unique implementation"); | |
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