【发布时间】:2018-09-13 18:21:12
【问题描述】:
就性能而言,第 1 名或第 2 名哪个更好?
const getCookieValue = readCookie('my_var') 应该声明在顶部,或者因为它的使用只是在一个条件下,所以最好将它保留在 if 语句中
方法 1
componentWillMount() {
const {data1, data2} = this.props
if(data1) {
const getCookieValue = readCookie('my_var')
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
或
方法 2
componentWillMount() {
const {data1, data2} = this.props
const getCookieValue = readCookie('my_var')
if(data1) {
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
【问题讨论】:
-
性能方面,仅在需要该变量的逻辑路径上执行操作会更快。但在这种情况下几乎可以忽略不计
标签: javascript reactjs performance ecmascript-6 react-lifecycle