Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <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 >
Setup JS var arr = [];
for (var i = 0 ; i < 100000 ; i++) {
arr.push (Math .floor (Math .random ()*1000 ));
}
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 isDup = dups[el];
dups[el] = true ;
return !isDup;
});
}
Teardown JS