Object.defineProperty vs Direct assignment

Edit on GitHub

Setup

Tests

  • Object.defineProperty

    var target = {};
    var key = 'something';
    
    for (var i=0; i<1000; i++) {
        // property getter
        var getter = function () {
            return _val;
        };
        
        // property setter
        var setter = function (newVal) {    
            _val = newVal;
        };
        
        // Create new property with getter and setter
        Object.defineProperty(target, key+i, {
          get: getter,
          set: setter,
        });
    }
  • Direct assignment

    var target = {};
    var key = 'something';
    
    for (var i=0; i<1000; i++) {
        target[key+1] = undefined;
    }