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
I have changed the size of the arrays to 1000, and the range of numbers to be from 0 to 100000, (but only multiples of 100) so that there are a number of matches.
The original example had a very small array, where efficiency is not so important, unless you are doing many arrays.
It appears that much of the code had not been tested, so there were a number of trivial errors. I have fixed them, and verified to see that the results are identical.
<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 size=1000;
var arr = Array.apply(0,Array(size)).map(function(i,j){return Math.floor(Math.random()*size*100)});
var arrclone1 = [], arrclone2 = [], arrclone3 = [];
for (var i=0; i<size; i++)
arrclone1[i] = arrclone2[i] = arrclone3[i] = arr[i];
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;
}
Array.prototype.getUnique2 = function(){
var u = Object.create(null), a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u[this[i]]) continue;
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
Array.prototype.getUnique3 = function(){
var u = Object.create(null), a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(this[i] in u) 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]);
}
}
results.push(sorted_arr[sorted_arr.length - 1]);
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;
}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 |
| arr.getUnique2 | | ready |
| arr.getUnique3 | | ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.