Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS 性能tips #7

Open
smallnewer opened this issue Nov 27, 2014 · 4 comments
Open

JS 性能tips #7

smallnewer opened this issue Nov 27, 2014 · 4 comments

Comments

@smallnewer
Copy link
Owner

一些JS的方法有性能坑,平时可以不用管,在压榨性能的场景下,需要用到。此贴作为积累和记录。

@smallnewer
Copy link
Owner Author

千万别用forEach循环数组。性能超差。
http://jsperf.com/array-each1
http://jsperf.com/fastest-array-loops-in-javascript/267

@smallnewer
Copy link
Owner Author

常用运算测试:
http://jsperf.com/operation1
http://jsperf.com/operation2

常用运算总体不会特别慢,只是相对间作比较。只在大量运算的时候,可以用性能好的运算方式替换不好的运算方式,可以取得一些优化效果。

  1. 尽量避免 弦操作:sin asin atan 等。
  2. 除法没有乘法性能好,能用乘法尽量用乘法。
  3. 开方性能也不好,和乘法差距较大。不过比弦操作要好不少。
  4. random()获取随机数性能

@smallnewer
Copy link
Owner Author

部分JS性能测试:http://my.oschina.net/u/1401419/blog/191199

@smallnewer
Copy link
Owner Author

判断是否数组。
参照:http://jsperf.com/instanceof-array-vs-array-isarray/5
Array.isArray性能也是糟透。通用的toString方式是最慢的,比isArray慢几个量级。千万别用。
arr instanceof Array 比较靠谱
但是更快的选择是arr.constructor === Array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant