Underscore union vs forEach

Benchmark created by Marsup on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>

Setup

var arrays = _.times(5, _.range.bind(_, 0, 50, 1));
    
    function manualUnion(arrays) {
      var union = [];
      arrays.forEach(function(array) {
        array.forEach(function(element) {
          if (union.indexOf(element) === -1) {
            union.push(element);
          }
        });
      });
      return union;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Underscore
_.union.apply(_, arrays);
ready
Manual
manualUnion(arrays);
ready
Underscore (iteration)
var result = [];
arrays.forEach(function(array) {
  result = _.union(result, array);
});
ready

Revisions

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

  • Revision 1: published by Marsup on