美文网首页
前端性能优化:js中优化条件判断语句

前端性能优化:js中优化条件判断语句

作者: 海布里的冬季 | 来源:发表于2020-11-16 09:01 被阅读0次

在开发过程中,由于追求开发速度,我们往往很多时候都没有注意代码的可读性与性能,这里介绍几个技巧,让你写出可读性强、简洁的js代码

1、多个条件满足之一时,推荐使用Array.includes

// 优化前
function test(val) {
    if (val === 'js' || val === 'java' || val === 'python') {
        console.log('编程语言')
    }
}

// 优化后
function test(val) {
    cosnt arr = ['js', 'java', 'python']
    if (arr.includes(val)) {
        console.log('编程语言')
    }
}

2、减少嵌套,尽早返回

// 优化前
function test(val) {
    if (val) {
        if (val === 'js') {
            console.log(val)
        } else {
            console.log('其他')
        }
    } else {
        return
    }
}

// 优化后
function test(val) {
    if (!val) return
    val === 'js' ? console.log(val) : console.log('其他')
}

3、使用函数的默认参数与解构

// 优化前
function test(val, num) {
    const item = num || 1
    console.log(`this is ${item}${val}`)
}
test('js', 4)

// 优化后
function test(val, num = 1) {
    console.log(`this is ${num}${val}`)
}
test('java', 4)

如果默认参数是对象呢?我们就可以使用解构了

// 优化前
function test(val) {
    if (val && val.name) {
        console.log(val.name)
    } else {
        console.log('null')
    }
}
test({name: 'js', num: 1})

// 优化后
function test({name} = {}) {
    console.log(name)
}
test({name: 'js', num: 1})

4、使用map或者对象字面量替代switch语句

// 优化前
function test(num) {
    switch(num) {
        case 1:
            return ['js', 'java']
        case 2:
            return ['python', 'ruby']
        case 3:
            return ['php', 'c#']
        default
    }
    conosle.log(num)
}
test(1)

// 优化后,对象字面量方式
function test(num) {
    const arr = {
        1: ['js', 'java'],
        2: ['python', 'ruby'],
        3: ['php', 'c#'],
    }
    console.log(arr[num])
}
test(1)

// map方式
function test(num) {
    const arr = new Map()
        .set(1, ['js', 'java'])
        .set(2, ['python', 'ruby'])
        .set(3, ['php', 'c#'])
    console.log(arr.get(num))
}
test(1)

5、使用Array.every()或者Array.some()

// 优化前
const item = [
    {name: 'js', num: 2},
    {name: 'java', num: 4},
    {name: 'pyton', num: 2},
    {name: 'php', num: 1},
]
function test() {
    let isNumTwo = true
    for (let val of item) {
        if (!isNumTwo) break
        isNumTwo = (val.num === 2)
    }
    console.log(isNumTwo)  // false
}
test()

// 优化后
const item = [
    {name: 'js', num: 2},
    {name: 'java', num: 4},
    {name: 'pyton', num: 2},
    {name: 'php', num: 1},
]
function test() {
    const isNumTwo = item.some(val => val.num === 2)
    console.log(isNumTwo)  // true
}
test()

相关文章

  • 前端性能优化:js中优化条件判断语句

    在开发过程中,由于追求开发速度,我们往往很多时候都没有注意代码的可读性与性能,这里介绍几个技巧,让你写出可读性强、...

  • 前端性能优化(下)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(中)...

  • 前端性能优化(中)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(上)...

  • 前端性能优化(上)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 为什么要进行前端性能...

  • 前端性能优化

    js性能小贴士——优化循环 前端网页与js性能优化 我总结的js性能优化的小知识 提高 web 应用性能之 Jav...

  • 前端性能 优化 大全

    js性能小贴士——优化循环 前端网页与js性能优化 我总结的js性能优化的小知识 提高 web 应用性能之 Jav...

  • 前端性能优化

    对于前端的性能优化,主要有分为加载优化和性能优化。在Google官方文档中,给出了性能优化有哪些好处。前端优化的最...

  • 性能优化

    +WEB前端性能优化:HTML,CSS,JS和服务器端优化对前端开发工程师来说,前端性能优化的重要性是不言而喻的,...

  • 前端性能优化原理与实践(三)

    摘自前端性能优化原理与实践 DOM 优化原理与基本实践 JS是很快的,在 JS中修改DOM对象也是很快的。在JS的...

  • 前端面试必问及加分点---性能优化篇

    如何进行网站性能优化 你有用过哪些前端性能优化的方法? 谈谈性能优化问题 代码层面的优化 前端性能优化最佳实践

网友评论

      本文标题:前端性能优化:js中优化条件判断语句

      本文链接:https://www.haomeiwen.com/subject/ograbktx.html