使用'new'调用函数比不使用它更便宜?

Mat*_*lor 12 javascript constructor class prototypal-inheritance

鉴于这种非常熟悉的原型构造模型:

function Rectangle(w,h) {
    this.width = w;
    this.height = h;
}
Rectangle.prototype.area = function() { 
    return this.width * this.height;
};
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么呼叫new Rectangle(2,3)持续比Rectangle(2,3)没有'new'关键字呼叫快10倍?我会假设因为new会通过涉及原型来增加函数执行的复杂性,所以它会更慢.

例:

var myTime;
function startTrack() {
    myTime = new Date();
}
function stopTrack(str) {
    var diff = new Date().getTime() - myTime.getTime();
    println(str + ' time in ms: ' + diff);
}

function trackFunction(desc, func, times) {
    var i;
    if (!times) times = 1;
    startTrack();
    for (i=0; i<times; i++) {
        func();
    }
    stopTrack('(' + times + ' times) ' + desc);
}

var TIMES = 1000000;

trackFunction('new rect classic', function() {
    new Rectangle(2,3);
}, TIMES);

trackFunction('rect classic (without new)', function() {
    Rectangle(2,3);
}, TIMES);
Run Code Online (Sandbox Code Playgroud)

收益率(在Chrome中):

(1000000 times) new rect classic time in ms: 33
(1000000 times) rect classic (without new) time in ms: 368

(1000000 times) new rect classic time in ms: 35
(1000000 times) rect classic (without new) time in ms: 374

(1000000 times) new rect classic time in ms: 31
(1000000 times) rect classic (without new) time in ms: 368
Run Code Online (Sandbox Code Playgroud)

Poi*_*nty 17

当您在没有"新"的情况下调用该函数时,您怀疑"这个"指向的是什么?它将是"窗口".更新比使用"new"调用它时更新新建的新对象要慢.

将第二个版本更改为:

trackFunction('rect classic (without new)', function() {
    Rectangle.call({}, 2,3);
}, TIMES);
Run Code Online (Sandbox Code Playgroud)

看看你得到了什么.另一件事是这样的:

trackFunction('rect with constant object', (function() {
  var object = { height: 0, width: 0 };
  return function() {
    Rectangle.call(object, 2, 3);
  };
})());
Run Code Online (Sandbox Code Playgroud)

这将节省每次迭代重建虚拟对象的成本.