jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Low number of duplicates
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
var arr = [];
for (var i = 0; i < 10000; i++) {
arr.push(Math.floor(Math.random()*20000));
}
var arrclone1 = arr.slice();
var arrclone2 = arr.slice();
var arrclone3 = arr.slice();
var jqFilter = function(v, k){
return $.inArray(v ,arr) === k;
}
Array.prototype.unique = function() {
var a = [];
for (var i=0, l=this.length; i<l; i++)
if (a.indexOf(this[i]) === -1)
a.push(this[i]);
return a;
}
Array.prototype.getUnique = function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
Array.prototype.sortFilter = function() {
var sorted_arr = this.sort();
var results = [];
for (var i = 0; i < this.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
Array.prototype.sortUnique = function() {
this.sort();
var last_i;
for (var i=0;i<this.length;i++)
if ((last_i = this.lastIndexOf(this[i])) !== i)
this.splice(i+1, last_i-i);
return this;
}
Array.prototype.distinct = function () {
var dups = {};
return this.filter(function(el) {
var hash = el.valueOf();
var isDup = dups[hash];
dups[hash] = true;
return !isDup;
});
}
Array.prototype.distinct2 = function () {
var dups = {};
var hash;
return this.filter(function(el) {
hash = el.valueOf();
var isDup = dups[hash];
dups[hash] = true;
return !isDup;
});
}
Array.prototype.distinct3 = function () {
var dups = {};
var hash, isDup;
return this.filter(function(el) {
hash = el.valueOf();
isDup = dups[hash];
dups[hash] = true;
return !isDup;
});
}
Ready to run.
Test | Ops/sec | |
---|---|---|
jQuery grep |
| ready |
Underscore uniq |
| ready |
My arr.unique |
| ready |
Rafael's arr.getUnique |
| ready |
TLindig's filter |
| ready |
swilliams' sorted testing |
| ready |
My version of sorted testing |
| ready |
Sorting my arr.unique |
| ready |
Distinct |
| ready |
Distinct2 |
| ready |
Distinct3 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.