Home  >  Article  >  Web Front-end  >  13 个JavaScript 性能提升技巧分享_javascript技巧

13 个JavaScript 性能提升技巧分享_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:51:271140browse

Daniel Clifford 在 Google I/O 2012 上做了一个精彩的演讲“Breaking the JavaScript Speed Limit
with V8”。在演讲中,他深入解释了13个简单的代码优化方法,可以让你的代码在 Chrome 的 V8 JavaScript 引擎编译/运行你的 JavaScript 代码更加快速。在演讲中,他解释了怎么优化,并且解释了原因。下面简明的列出了优化的要点:

  1. 在构造函数中初始化所有的对象

  2. 始终用相同的顺序初始化对象

  3. 尽量使用数字

  4. 为数组使用从0开始的连续的主键

  5. 不要提前分配大型数组(>64K),你应该在使用的过程中扩大数组

  6. 不要删除数组中的元素

  7. 不要装在没有初始化或者已经删除的元素

  8. 对于固定大小的数组,使用”array literals“初始化

  9. 在使用前分配正确的空间大小给小数组

  10. 不要在数值数组中存储非数值内容

  11. 尽量使用单一类型(monomorphic)而不是多类型(polymorphic)

  12. 不要使用 try{} catch{}

  13. 在优化后避免在方法中修隐藏的类

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn