Underscore Map vs Array.prototype.map

Benchmark created by L8D on


Description

Obviously these tests are very useful as we know that Underscore.js defaults to Array.prototype.map if the given list passes instanceof Array test.

Preparation HTML

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

Setup

var output, marr = []; /* output variable to prevent possible no-op optimizations by V8 */
    for (var i = 0; i <= 30; i++) {
      marr.push(~~(Math.random() * 256));
    }
    
    var double = function(n) {
      return n * 2;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Underscore
output = _.map(marr, double);
ready
Array.prototype.map
output = marr.map(double);
ready
For loop with function call
output = [];
for (var i = 0, l = marr.length; i < l; i++) {
  output[i] = double(marr[i]);
}
ready
For loop
output = [];
for (var i = 0, l = marr.length; i < l; i++) {
  output[i] = marr[i] * 2;
}
ready

Revisions

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