5

source

define two object both with getter and setter, with the same code

both test with benchmark.js in node v7.3.0

const builtInObject1 = (function (object) {
    let lastA = 1;
    return Object.defineProperties(object, {
        a:{
            get(){
                return lastA
            },
            set(newValue){
                lastA = newValue;
            }
        }
    })
})({});

const builtInObject2 = (function (object) {
    let lastA = 1;
    return Object.defineProperties(object, {
        a:{
            get(){
                return lastA
            },
            set(newValue){
                lastA = newValue;
            }
        }
    })
})({});

~ addes2015'sgetter/settercase

const builtInObject3 = (function () {
    let lastA = 1;
    return {
        get a(){
            return lastA
        },
        set a(value){
            lastA = value;
        }
    }
})();

~

const builtInObject4 = (function (object) {
    let last = 1;
    return Object.defineProperties(object, {
        b:{
            get(){
                return last
            },
            set(newValue){
                last = newValue;
            }
        }
    })
})({});

const builtInObject5 = (function (object) {
    let last = 1;
    return Object.defineProperties(object, {
        c:{
            get(){
                return last
            },
            set(newValue){
                last = newValue;
            }
        }
    })
})({});

~

(new Benchmark.Suite("object-assign-properties"))
    .add("#built-in object1.a getter and setter", function () {
        builtInObject1.a = builtInObject1.a + 1;
    })
    .add("#built-in object2.a getter and setter", function () {
        builtInObject2.a = builtInObject2.a + 1;
    })
    .add("#built-in object3.a es6 getter and setter", function () {
        builtInObject3.a = builtInObject3.a + 1;
    })
    .add("#built-in object4.b getter and setter", function () {
        builtInObject4.b = builtInObject4.b + 1;
    })
    .add("#built-in object5.c getter and setter", function () {
        builtInObject5.c = builtInObject5.c + 1;
    })
    .on('cycle', function(event) {
        console.log(String(event.target));
    })
    .on('complete', function() {
        console.log('Fastest is ' + this.filter('fastest').map('name'));
    })
    .run({ 'async': false });

result

#built-in object1.a getter and setter x 80,459,419 ops/sec ±0.65% (88 runs sampled)
#built-in object2.a getter and setter x 3,967,313 ops/sec ±0.36% (91 runs sampled)
#built-in object3.a es6 getter and setter x 3,982,725 ops/sec ±0.51% (93 runs sampled)
#built-in object4.b getter and setter x 79,608,022 ops/sec ±4.06% (87 runs sampled)
#built-in object5.c getter and setter x 78,849,808 ops/sec ±0.82% (92 runs sampled)
Fastest is #built-in object1.a getter and setter

difference between ops/sec of these tests result make me confuse

Why???

what makes these differences ?

accroding to bluebird | Optimization-killers, object3 would not be optimized, but why object2 get so slow ??

reference ???

related links

tommyZZM
  • 83
  • 6
  • What if you run the test for `builtInObject2` first? – zerkms Jan 15 '17 at 04:18
  • 1
    .. so why are you asking about the object performance and not about the test suite you are using when performance difference follow the way you organize the tests? – Soren Jan 15 '17 at 05:43
  • @zerkms regardless of whether or not it is run first, if define object2 first, then object2 would be faste. whatever test runing first or second .. – tommyZZM Jan 15 '17 at 06:34
  • @Soren I'm not sure its javascript behavior or test suite problem. – tommyZZM Jan 15 '17 at 06:38
  • @zerkms @Soren if object1 and object2 define setter/getter property with two different name ( like: `object1.a` and `object2.b` ), then the benchmark will the `same` result – tommyZZM Jan 15 '17 at 07:42

0 Answers0