Dataset Join

JavaScript performance comparison

Revision 2 of this test case created by Alex

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>

<script>
$.noConflict();
var generateArray = function(size) {
  var FYShuffle = function(array) {
    var m = array.length, t, i;
    while (m) {
      i = Math.floor(Math.random() * m--);
      t = array[m];
      array[m] = array[i];
      array[i] = t;
    }
  };
  var a = [];
  var length = size * 3;
  for (var i = 0; i < length; i++) {
    a.push(i);
  }
  FYShuffle(a);
  return a.slice(0,size);
}
var size = 100;
var t1 = generateArray(size);
var t2 = generateArray(size);
var a1 = {};
var a2 = {};
for (var i = 0; i < t1.length; i++) {
  a1[t1[i]] = {};
}
for (var i = 0; i < t2.length; i++) {
  a2[t2[i]] = {};
}
</script>
Join set lengths, these should be identical.
<br>
Test 1 (Standard (for) Array) joined: <span id="test1"></span>
<br>
Test 2 (Standard (every) Array) joined: <span id="test2"></span>
<br>
Test 3 (jQuery) joined: <span id="test3"></span>
<br>
Test 4 (Prototype) joined: <span id="test4"></span>
<br>
Test 5 (Underscore) joined: <span id="test5"></span>
<br>
Test 6 (Underscore - Every) joined: <span id="test6"></span>
<br>
Test 7 (Standard Index) joined: <span id="test7"></span>
<br>
Test 8 (Standard Build+Index) joined: <span id="test8"></span>
<br>
Test 9 (Underscore - Intersection) joined: <span id="test9"></span>
<br>
Test 10 (Underscore - Reduce) joined: <span id="test10"></span>
<br>
Test 11 (Underscore - Filter) joined: <span id="test11"></span>
<br>
Test 12 (Standard (while) Array) joined: <span id="test12"></span>
    

Preparation code output

Join set lengths, these should be identical. <br> Test 1 (Standard (for) Array) joined: <span id="test1"></span> <br> Test 2 (Standard (every) Array) joined: <span id="test2"></span> <br> Test 3 (jQuery) joined: <span id="test3"></span> <br> Test 4 (Prototype) joined: <span id="test4"></span> <br> Test 5 (Underscore) joined: <span id="test5"></span> <br> Test 6 (Underscore - Every) joined: <span id="test6"></span> <br> Test 7 (Standard Index) joined: <span id="test7"></span> <br> Test 8 (Standard Build+Index) joined: <span id="test8"></span> <br> Test 9 (Underscore - Intersection) joined: <span id="test9"></span> <br> Test 10 (Underscore - Reduce) joined: <span id="test10"></span> <br> Test 11 (Underscore - Filter) joined: <span id="test11"></span> <br> Test 12 (Standard (while) Array) joined: <span id="test12"></span>

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
Standard JS (for) - Array
var o = [];
for (var i = 0; i < t1.length; i++) {
  for (var j = 0; j < t2.length; j++) {
    if (t1[i] === t2[j]) {
      o.push(t1[i]);
      break
    }
  }
}
document.getElementById('test1').innerHTML = o.length;
pending…
Standard JS (every) - Array
var o = [];
t1.every(function(v) {
  t2.every(function(w) {
    if (v === w) {
      o.push(v);
      return false;
    }
    return true;
  });
  return true;
});
document.getElementById('test2').innerHTML = o.length;
pending…
jQuery - Array
var o = [];
jQuery.each(t1, function(i, v) {
  jQuery.each(t2, function(j, w) {
    if (v === w) {
      o.push(v);
      return false;
    }
  });
});
document.getElementById('test3').innerHTML = o.length;
pending…
Prototype - Array
var o = [];
t1.each(function(v) {
  t2.each(function(w) {
    if (v === w) {
      o.push(v);
      throw $break;
    }
  });
});
document.getElementById('test4').innerHTML = o.length;
pending…
Underscore - Array
var o = [];
_.each(t1, function(v) {
  _.each(t2, function(w) {
    if (v === w) {
      o.push(v);
    }
  });
});
document.getElementById('test5').innerHTML = o.length;
pending…
Underscore - Array (Every)
var o = [];
t1.every(function(v) {
  t2.every(function(w) {
    if (v === w) {
      o.push(v);
      return false;
    }
    return true;
  });
  return true;
});
document.getElementById('test6').innerHTML = o.length;
pending…
Standard JS - Index
var o = [];
for (var i in a1) {
  if (typeof a2[i] !== 'undefined') {
    o.push(i);
  }
}
document.getElementById('test7').innerHTML = o.length;
pending…
Standard JS - Build+Index
var b1 = {};
var b2 = {};
for (var i = 0; i < t1.length; i++) {
  b1[t1[i]] = {};
}
for (var i = 0; i < t2.length; i++) {
  b2[t2[i]] = {};
}

var o = [];
for (var i in b1) {
  if (typeof b2[i] !== 'undefined') {
    o.push(i);
  }
}
document.getElementById('test8').innerHTML = o.length;
pending…
Underscore - Intersection
var o = _.intersection(t1, t2);
document.getElementById('test9').innerHTML = o.length;
pending…
Underscore - Reduce
var o = _.reduce(t1, function(memo, num){ 
  if( _.indexOf(t2, num) !== -1 ) memo.push(num);
  return memo;
}, []);

document.getElementById('test10').innerHTML = o.length;
pending…
Underscore - Filter
var o = _.filter(t1, function(num){ return _.indexOf(t2, num) !== -1 });

document.getElementById('test11').innerHTML = o.length;
pending…
Standard (while) Array
var i = 0, j = 0, o = [];
t1 = _.sortBy(t1, function(num){ return num; });
t2 = _.sortBy(t2, function(num){ return num; });

while( i < t1.length && j < t2.length ){
  if( t1[i] < t2[j] ) i++;
  else if( t1[i] > t2[j] ) j++;
  else { o.push(t1[i]); i++; j++; }
}

document.getElementById('test12').innerHTML = o.length;
pending…

Compare results of other browsers

Revisions

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

0 Comments

Join set lengths, these should be identical.
Test 1 (Standard (for) Array) joined:
Test 2 (Standard (every) Array) joined:
Test 3 (jQuery) joined:
Test 4 (Prototype) joined:
Test 5 (Underscore) joined:
Test 6 (Underscore - Every) joined:
Test 7 (Standard Index) joined:
Test 8 (Standard Build+Index) joined:
Test 9 (Underscore - Intersection) joined:
Test 10 (Underscore - Reduce) joined:
Test 11 (Underscore - Filter) joined:
Test 12 (Standard (while) Array) joined: