unique-array

JavaScript performance comparison

Test case created by Volland

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>



      
<script>
Benchmark.prototype.setup = function() {
   const rnd = () => Math.floor((Math.random() * 10000000) + 1) % 200;
  
  var a10 = [] ; a200 = [] ; a10000 = [];
  var i = 0;
  
  while (i++ < 10000 ) {
    a10000[i] = rnd();
    if (i < 200) {a200[i] =  rnd()}
    if  (i < 10) {a10[i] =  rnd()}
  }
  
  function uIndexOf (arr) {
   var r = [];
   var l = arr.length ; 
  for(var j = 0 ; j < l ; j++) {
    if(r.indexOf(arr[j]) === -1) {
        r.push(arr[j]);
    }
  }
  return r;
  }
  
  function uExtraMap (arr) {
  var r = [];
  var m = {}
  
   var l = arr.length ; 
  for(var j = 0 ; j < l ; j++) {
  
    if(!m[arr[j]]) {
        m[arr[j]] = true;
        r.push(arr[j]);
    }
  }
  return r;
  }
  function uExtraMapKeys (arr) {
  var m = {}
  var l = arr.length;
  for(var j = 0 ; j < l ; j++) {
     m[arr[j]] = true;
  }
  return Object.keys(m);
  }
  
  function uMapForIn (arr) {
  var m = {}
  var l = arr.length;
  for(var j = 0 ; j < l ; j++) {
     m[arr[j]] = true;
  }
  var r = [];
  for(v in m) {
   r.push(v);
  }
  return r;
  }
  function uMapInProp (arr) {
  var r = [];
  var m = {}
  
   var l = arr.length ; 
  for(var j = 0 ; j < l ; j++) {
  
    if(!(arr[j] in m)) {
        m[arr[j]] = true;
        r.push(arr[j]);
    }
  }
  return r;
  }
  
    function onlyUnique(value, index, self) { 
        return self.indexOf(value) === index;
    }
  function uSelfIndexOf(a) {
  
    return a.filter( onlyUnique );;
  }
  function setToArray (arr) {
    return [...new Set(arr)];
  }
  function cmp(a,b){
   return a - b;
  }
  function sortedUnique (arr) {
      arr.sort(cmp);
      var r = [];
      for (var i = 0, l = arr.length - 1 ; i < l; i++ ) {
          if(arr[i] - arr[i+1]) {
               r.push(arr[i]);
          }
      }
      return r ;
  }
  
  function sortedUniqueNoMutate (arr) {
      var a = arr.slice(0);
      a.sort(cmp);
      var r = [];
      for (var i = 0, l = arr.length - 1 ; i < l; i++ ) {
          if(a[i] - a[i+1]) {
               r.push(a[i]);
          }
      }
      return r ;
  }

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
indexOf
uIndexOf(a10000);
pending…
map
uExtraMap(a10000);
pending…
map in
uExtraMapKeys(a10000);
pending…
self filter
uSelfIndexOf(a10000)
pending…
okeys
uExtraMapKeys(a10000)
pending…
forin
uMapForIn(a10000)
pending…
set to array
setToArray(a10000)
pending…
sorted
sortedUnique(a10000)
pending…
sortedUniqueNoMutate
sortedUniqueNoMutate(a10000)
pending…
lodash
_.uniq(a10000)
pending…
ramda
R.uniq(a10000)
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments