HTML5技术

GitHub 上一份很受欢迎的前端代码优化指南-强烈推荐收藏 - yonghu86(2)

字号+ 作者:H5之家 来源:H5之家 2015-12-04 15:46 我要评论( )

/* bad */div { transition: all 1s; top: 50%; margin-top: -10px; padding-top: 5px; padding-right: 10px; padding-bottom: 20px; padding-left: 10px;}/* good */div { transition: 1s; top: calc(50% - 10px);

/* bad */ div { transition: all 1s; top: 50%; margin-top: -10px; padding-top: 5px; padding-right: 10px; padding-bottom: 20px; padding-left: 10px; } /* good */ div { transition: 1s; top: calc(50% - 10px); padding: 5px 10px 20px; }

语言

能用英文的时候不用数字。

/* bad */ :nth-child(2n + 1) { transform: rotate(360deg); } /* good */ :nth-child(odd) { transform: rotate(1turn); }

供应商的前缀

砍掉过时的供应商前缀。必须使用时,需要放在标准属性前:

/* bad */ div { transform: scale(2); -webkit-transform: scale(2); -moz-transform: scale(2); -ms-transform: scale(2); transition: 1s; -webkit-transition: 1s; -moz-transition: 1s; -ms-transition: 1s; } /* good */ div { -webkit-transform: scale(2); transform: scale(2); transition: 1s; }

动画

除了变形和改变透明度用animation,其他尽量使用transition。

/* bad */ div:hover { animation: move 1s forwards; } @keyframes move { 100% { margin-left: 100px; } } /* good */ div:hover { transition: 1s; transform: translateX(100px); }

单位

可以不用单位时就不用。建议用rem。时间单位用s比ms好。

/* bad */ div { margin: 0px; font-size: .9em; line-height: 22px; transition: 500ms; } /* good */ div { margin: 0; font-size: .9rem; line-height: 1.5; transition: .5s; }

颜色

需要做透明效果是用rgba,否则都用16进制表示:

/* bad */ div { color: hsl(103, 54%, 43%); } /* good */ div { color: #5a3; }

绘图

减少HTTPS请求,尽量用CSS绘图替代图片:

/* bad */ div::before { content: url(white-circle.svg); } /* good */ div::before { content: ""; display: block; width: 20px; height: 20px; border-radius: 50%; background: #fff; }

注释

/* bad */ div { // position: relative; transform: translateZ(0); } /* good */ div { /* position: relative; */ will-change: transform; }

JavaScript 性能

有可读性、正确性和好的表达比性能更重要。JavaScript基本上不会是你的性能瓶颈。有些可优化细节例如:图片压缩、网络接入、DOM文本流。如果你只能记住本指南的一条规则,那就记住这条吧。

// bad (albeit way faster) const arr = [1, 2, 3, 4]; const len = arr.length; var i = -1; var result = []; while (++i < len) { var n = arr[i]; if (n % 2 > 0) continue; result.push(n * n); } // good const arr = [1, 2, 3, 4]; const isEven = n => n % 2 == 0; const square = n => n * n; const result = arr.filter(isEven).map(square);

Statelessness

尽量保持代码功能简单化,每个方法都对其他其他代码没有负影响。不使用外部数据。返回一个新对象而不是覆盖原有的对象。

// bad const merge = (target, ...sources) => Object.assign(target, ...sources); merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" } // good const merge = (...sources) => Object.assign({}, ...sources); merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" }

尽量使用内置方法

// bad const toArray = obj => [].slice.call(obj); // good const toArray = (() => Array.from ? Array.from : obj => [].slice.call(obj) )();

严格条件

在非必要严格条件的情况不要使用。

// bad if (x === undefined || x === null) { ... } // good if (x == undefined) { ... }

对象

不要在循环里强制改变对象的值,,可以利用array.prototype方法。

// bad const sum = arr => { var sum = 0; var i = -1; for (;arr[++i];) { sum += arr[i]; } return sum; }; sum([1, 2, 3]); // => 6 // good const sum = arr => arr.reduce((x, y) => x + y); sum([1, 2, 3]); // => 6 If you can't, or if using array.prototype methods is arguably abusive, use recursion. // bad const createDivs = howMany => { while (howMany--) { document.body.insertAdjacentHTML("beforeend", "<div></div>"); } }; createDivs(5); // bad const createDivs = howMany => [...Array(howMany)].forEach(() => document.body.insertAdjacentHTML("beforeend", "<div></div>") ); createDivs(5); // good const createDivs = howMany => { if (!howMany) return; document.body.insertAdjacentHTML("beforeend", "<div></div>"); return createDivs(howMany - 1); }; createDivs(5);

Arguments

忘记arguments对象吧,其他参数是更好的选择,因为:

  • 它已经被定义
  • 它是一个真的数组,很方便使用

    // bad const sortNumbers = () => Array.prototype.slice.call(arguments).sort(); // good const sortNumbers = (...numbers) => numbers.sort();

  • Apply

    忘记apply(),改用运算操作。

    const greet = (first, last) => `Hi ${first} ${last}`; const person = ["John", "Doe"]; // bad greet.apply(null, person); // good greet(...person);

    Bind

    不用bind()方法,这有更好的选择:

    // bad ["foo", "bar"].forEach(func.bind(this)); // good ["foo", "bar"].forEach(func, this); // bad const person = { first: "John", last: "Doe", greet() { const full = function() { return `${this.first} ${this.last}`; }.bind(this); return `Hello ${full()}`; } } // good const person = { first: "John", last: "Doe", greet() { const full = () => `${this.first} ${this.last}`; return `Hello ${full()}`; } }

    更好的排序

    避免多重嵌套:

    // bad [1, 2, 3].map(num => String(num)); // good [1, 2, 3].map(String);

    Composition

    避免方法嵌套调用,改用composition:

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3 - sheng.chao

      GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3 - shen

      2017-03-22 13:02

    • 一份关于组建.NET Core开源团队的倡议书 - 彭泽0902

      一份关于组建.NET Core开源团队的倡议书 - 彭泽0902

      2017-03-13 17:02

    • 一个想法(续三):一份IT技术联盟创业计划书,开启众筹创业征程 - 路过秋天

      一个想法(续三):一份IT技术联盟创业计划书,开启众筹创业征程 -

      2017-02-10 11:00

    • 【福利大放送】不止是Android,Github超高影响力开源大放送,学习开发必备教科书 - 南尘

      【福利大放送】不止是Android,Github超高影响力开源大放送,学习开

      2017-02-09 12:04

    网友点评