Advertisement
rg443

javascript array unique benchmarks

Jan 26th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   var arr = [];
  2.  
  3.   for (var i = 0; i < 1000; i++) {
  4.       arr.push(i);
  5.  
  6.       if (i > 0 && i % 10 == 0) {
  7.           arr.push(i - 10);
  8.       }
  9.   }
  10.  
  11.   // https://twitter.com/judofyr/status/83901970494144513
  12.   Array.prototype.judofyr=function(a,r,c,d,i){for(a=this.sort(),i=0,r=[a[i]];c=a[i++],d=a[i],i<this.length;)c===d||r.push(d);return r}
  13.  
  14.   // https://twitter.com/WebReflection/status/83867362490720258
  15.   Array.prototype.WebReflection=function(a){return function(){return this.filter(a)}}(function(a,b,c){return c.indexOf(a,b+1)<0});
  16.  
  17.   // https://gist.github.com/1042640
  18.   Array.prototype.abozhilov = (function () {
  19.       var TYPE_MAP = {
  20.           'object' : 1,
  21.           'function' : 1
  22.       }
  23.       return function () {
  24.           var map = {},
  25.               objects = [],
  26.               unique = [];
  27.           for (var i = 0, len = this.length; i < len; i++) {
  28.               var val = this[i];
  29.              
  30.               if (TYPE_MAP[typeof val] && val) {
  31.                   if (!val.__unique__) {
  32.                       unique.push(val);
  33.                       objects.push(val);
  34.                       val.__unique__ = 1;
  35.                   }
  36.               }
  37.               else if (!map[val]) {
  38.                   unique.push(val);
  39.                   map[val] = 1;
  40.               }
  41.           }
  42.          
  43.           for (i = objects.length; i--;) {
  44.               delete objects[i].__unique__;
  45.           }
  46.          
  47.           return unique;
  48.       };
  49.   })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement