Javascript引擎优化

问题描述 投票:0回答:1

如果我在 Javascript 中有以下代码:

import getTranslation from "some-library";
import getUserSettings from "some-other-library";

const getTranslatedLabel = () => {
  const translatedLabel = getTranslation("cheeseburger");
  const { enableTranslatedLabels } = getUserSettings();

  if (enableTranslatedLabels) {
     return translatedLabel;
  } else {
     return "";
  }
}

像 V8 这样的 javascript 引擎是否足够智能,仅在enableTranslatedLabels 为

getTranslation
时才执行
true
函数?

javascript performance v8
1个回答
0
投票

这和V8无关,更多的是和你的业务逻辑有关。

您需要重新排列代码,以便仅在启用时执行该功能。

import getTranslation from "some-library";
import getUserSettings from "some-other-library";

const getTranslatedLabel = () => {

  const { enableTranslatedLabels } = getUserSettings();

  //if not enabled, we will not executed getTranslation
  if( enableTranslatedLabels ){
     return "";
  }

  //this will be executed and value returned because it is enabled.
  return getTranslation("cheeseburger");

}
© www.soinside.com 2019 - 2024. All rights reserved.