素材牛VIP会员
两个`一模一样`的object先后定义为什么会有巨大的性能差异?
 54***66  分类:Node.js  人气:1180  回帖:1  发布于6年前 收藏

先后定义两个带settergetter的对象, 然后在node v7.3.0, 用benchmark.js测试

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;
            }
        }
    })
})({});
//补充es6的`get`和`set`属性定义方法
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 });

结果

#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

object1object2的定义方式是一模一样的,但object1object2object3快十多倍!

2017-01-15 19:41

补充了object4object5, 的执行效率和object1几乎是一样的。

根据 <bluebird | Optimization-killers (译文)> object3即es6的写法目前仍然不会被优化, 但是问题还是回到了为什么object2object1相比会慢这么多呢???

为什么呢 ???

关联连接

  • stackoverflow | what makes two same objects performance different?

  • github/nodejs/help | #442 (大家可以关注这个issue)

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv5 码农
me***20 技术总监 6年前#1
 文明上网,理性发言!   😉 阿里云幸运券,戳我领取