native vs lodash (v6)

Revision 6 of this benchmark created on


Description

Just doing some benchmarks.

Preparation HTML

<script src="//rawgithub.com/lodash/lodash/9bb9cee4cbf4e4780ba4a16dae717ccaf344dd5b/dist/lodash.js"></script>

Setup

var _ = window._,
      a = _.shuffle(_.range(1000));
    
    function process(i) {
      return i * i + i;
    }
    
    
    // angular.map (1.3.0.rc2)
    
    var isArray = Array.isArray;
    
    function isString(value){return typeof value === 'string';}
    
    function isFunction(value){return typeof value === 'function';}
    
    function isObject(value){
      // http://jsperf.com/isobject4
      return value !== null && typeof value === 'object';
    }
    
    function isWindow(obj) {
      return obj && obj.window === obj;
    }
    
    function isArrayLike(obj) {
      if (obj == null || isWindow(obj)) {
        return false;
      }
    
      var length = obj.length;
    
      if (obj.nodeType === 1 && length) {
        return true;
      }
    
      return isString(obj) || isArray(obj) || length === 0 ||
             typeof length === 'number' && length > 0 && (length - 1) in obj;
    }
    
    function forEach(obj, iterator, context) {
      var key, length;
      if (obj) {
        if (isFunction(obj)) {
          for (key in obj) {
            // Need to check if hasOwnProperty exists,
            // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
            if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
              iterator.call(context, obj[key], key, obj);
            }
          }
        } else if (isArray(obj) || isArrayLike(obj)) {
          var isPrimitive = typeof obj !== 'object';
          for (key = 0, length = obj.length; key < length; key++) {
            if (isPrimitive || key in obj) {
              iterator.call(context, obj[key], key, obj);
            }
          }
        } else if (obj.forEach && obj.forEach !== forEach) {
            obj.forEach(iterator, context, obj);
        } else {
          for (key in obj) {
            if (obj.hasOwnProperty(key)) {
              iterator.call(context, obj[key], key, obj);
            }
          }
        }
      }
      return obj;
    }
    
    function map(obj, iterator, context) {
      var results = [];
      forEach(obj, function(value, index, list) {
        results.push(iterator.call(context, value, index, list));
      });
      return results;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
angular.map
var r = map(a, process);
ready
Array.map
var r = a.map(process);
ready

Revisions

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