What does compound let/const assignment mean?

☆樱花仙子☆ 提交于 2019-11-30 07:57:48

问题


There is an article Optimization killers in wiki of Bluebird library. In this article there is a phrase:

Currently not optimizable:
...
Functions that contain a compound let assignment
Functions that contain a compound const assignment

What does compound let assignment and compound const assignment mean? In ECMAScript 5.1 there was notion of compound assignment but in ECMAScript 2015, it seems there is no notion of any compound assignment there is only regular assignments.

I suspect that compound let and const assignment, it is just compound assignment after declaration. For example:

let n = 1;
n += 4;

Am I right?


回答1:


Yes, that seems to be exactly what it means. I had the following code (irrelevant additional lines removed):

function updatePlayer(player) {
    let direction = player.getDirection();
    if (player.isPressingLeft()) {
        direction += angleChange;
    }
    if (player.isPressingRight()) {
        direction -= angleChange;
    }
    player.setDirection(direction);
}

updatePlayer generated a warning, Not optimized: Unsupported let compound assignment, in Chrome's Profiles tab, so I tried = ... + instead of += and got a significant, consistent performance improvement.

jsPerf shows that let compound assignments are indeed extremely slow in Chrome 49.0.2623, compared to + ... = or var! I guess this will be fixed in a future version, since neither Firefox nor IE11 nor Edge is affected, and since Google apparently know about the issue.



来源:https://stackoverflow.com/questions/34595356/what-does-compound-let-const-assignment-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!