What is the difference between `new Object()` and object literal notation?

前端 未结 11 802
北恋
北恋 2020-11-22 07:41

What is the difference between this constructor-based syntax for creating an object:

person = new Object()

...and this literal syntax:

11条回答
  •  长发绾君心
    2020-11-22 08:19

    2019 Update

    I ran the same code as @rjloura on my OSX High Sierra 10.13.6 node version 10.13.0 and these are the results

    console.log('Testing Array:');
    console.time('using[]');
    for(var i=0; i<200000000; i++){var arr = []};
    console.timeEnd('using[]');
    
    console.time('using new');
    for(var i=0; i<200000000; i++){var arr = new Array};
    console.timeEnd('using new');
    
    console.log('Testing Object:');
    
    console.time('using{}');
    for(var i=0; i<200000000; i++){var obj = {}};
    console.timeEnd('using{}');
    
    console.time('using new');
    for(var i=0; i<200000000; i++){var obj = new Object};
    console.timeEnd('using new');
    
    
    Testing Array:
    using[]: 117.613ms
    using new: 117.168ms
    Testing Object:
    using{}: 117.205ms
    using new: 118.644ms
    

提交回复
热议问题