Object keys iteration (v97)

Revision 97 of this benchmark created by Fabio Costa on


Description

Testing Object.keys vs for-in. I see that SlickGrid uses objects with indexed keys rather than arrays. Confused....

Setup

var X = function() {
      this.x = 1;
      this.y = 4;
    };
    var data = [
    {foo: 'bar', bar: null, d: 3, f: 20, g: 30},
    {bar: null, 12312312: 30},
    new X(),
    {bar: null, 12312312: 30},
    new X(),
    {foo: 'bar', bar: null, d: 3, f: 20, g: 30}
    ];
    var ownProp = Object.prototype.hasOwnProperty;
    function _assign_forin(to, from) {
      for (var key in from) {
        if (hasOwnProperty.call(from, key)) {
          to[key] = from[key];
        }
      }
    }
    function _assign_keys(to, from) {
      var keys = Object.keys(from);
      for (var i = 0; i < keys.length; i++) {
        to[keys[i]] = from[keys[i]];
      }
    }
    
    function assign_forin(target, sources) {
      if (target == null) {
        throw new TypeError('Object.assign target cannot be null or undefined');
      }
      var to = Object(target);
      for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
        var nextSource = arguments[nextIndex];
        if (nextSource == null) {
          continue;
        }
        var from = Object(nextSource);
        for (var key in from) {
          if (hasOwnProperty.call(from, key)) {
            to[key] = from[key];
          }
        }
      }
      return to;
    }
    
    function assign_forin_separate_function(target, sources) {
      if (target == null) {
        throw new TypeError('Object.assign target cannot be null or undefined');
      }
      var to = Object(target);
      for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
        var nextSource = arguments[nextIndex];
        if (nextSource == null) {
          continue;
        }
        _assign_forin(to, Object(nextSource));
      }
      return to;
    }
    
    function assign_keys(target, sources) {
      if (target == null) {
        throw new TypeError('Object.assign target cannot be null or undefined');
      }
      var to = Object(target);
      for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
        var nextSource = arguments[nextIndex];
        if (nextSource == null) {
          continue;
        }
        var from = Object(nextSource);
        var keys = Object.keys(from);
        for (var i = 0; i < keys.length; i++) {
          to[keys[i]] = from[keys[i]];
        }
      }
      return to;
    }
    
    function assign_keys_separate_function(target, sources) {
      if (target == null) {
        throw new TypeError('Object.assign target cannot be null or undefined');
      }
      var to = Object(target);
      for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
        var nextSource = arguments[nextIndex];
        if (nextSource == null) {
          continue;
        }
        _assign_keys(to, Object(nextSource));
      }
      return to;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
assign_forin
for (var i = 0; i < data.length; i++) {
  assign_forin(data[i], data[i]);
}
ready
assign_forin_separate_function
for (var i = 0; i < data.length; i++) {
  assign_forin_separate_function(data[i], data[i]);
}
ready
assign_keys
for (var i = 0; i < data.length; i++) {
  assign_keys(data[i], data[i]);
}
ready
assign_keys_separate_function
for (var i = 0; i < data.length; i++) {
  assign_keys_separate_function(data[i], data[i]);
}
ready

Revisions

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