{"action":"created","issue":{"url":"https://api.github.com/repos/drduan/minggeJS/issues/201","labels_url":"https://api.github.com/repos/drduan/minggeJS/issues/201/labels{/name}","comments_url":"https://api.github.com/repos/drduan/minggeJS/issues/201/comments","events_url":"https://api.github.com/repos/drduan/minggeJS/issues/201/events","html_url":"https://github.com/drduan/minggeJS/issues/201","id":123941181,"number":201,"title":"到底try{}catch (e){}会影响执行效率吗？明哥来解答你","user":{"login":"minggeJS","id":16284765,"avatar_url":"https://avatars.githubusercontent.com/u/16284765?v=3","gravatar_id":"","url":"https://api.github.com/users/minggeJS","html_url":"https://github.com/minggeJS","followers_url":"https://api.github.com/users/minggeJS/followers","following_url":"https://api.github.com/users/minggeJS/following{/other_user}","gists_url":"https://api.github.com/users/minggeJS/gists{/gist_id}","starred_url":"https://api.github.com/users/minggeJS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minggeJS/subscriptions","organizations_url":"https://api.github.com/users/minggeJS/orgs","repos_url":"https://api.github.com/users/minggeJS/repos","events_url":"https://api.github.com/users/minggeJS/events{/privacy}","received_events_url":"https://api.github.com/users/minggeJS/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-12-26T17:41:50Z","updated_at":"2015-12-28T16:07:33Z","closed_at":null,"body":"到底try{}catch (e){}会影响执行效率吗？这个问题一直具有争议性，甚至我在一个群里骂得不可开交！几乎要打架！\r\n群里大部份人则觉得try{}catch (e){}会影响效率，应尽量避免使用！但事实又是不是这样呢！\r\n\r\n我们进行一个测试，为保证测试严谨性，我并没有使用任何测试软件，我是这样认为，一段JS是真实环境内运行的，我们应该尽量地模似一个真实环境！\r\n我统一使用的浏览器是chrome 最新版本，FOR 20000000 ，有人质疑了，为什么FOR   20000000次，你为什么不直接一次运行，我就草了！相差几微毫，怎么看！       \r\n大家都FOR  20000000次 很公平的，别说了！\r\n第一段代码:\r\n![1](https://cloud.githubusercontent.com/assets/16284765/12007334/7654ee00-ac3a-11e5-939b-28a57894c839.png)\r\n\r\n第一段代码运行正则 花时如图4082毫秒！正则还是有点吃力的！\r\n\r\n\r\n------------------------------------------------------------------------------------------------------\r\n\r\n第二段代码，我们用try{}catch (e){}\r\n![2](https://cloud.githubusercontent.com/assets/16284765/12007337/a15295f8-ac3a-11e5-8435-2a080239a081.png)\r\ntry只需要花时（如图）62毫秒,性能还很不错的！性能没外界传的那么差，我们什么都不要信，只相信自己的眼睛就可以，\r\n有人又质疑，是你测的，又不是我测的，那你自己可以去测啊！呵呵\r\n\r\n--------------------------------------------------------------------------------------------------------\r\n\r\n\r\n第三段代码，我什么都不要加，直接运行\r\n![3](https://cloud.githubusercontent.com/assets/16284765/12007342/be8db346-ac3a-11e5-9d16-655b95d5196c.png)\r\n直接运行果然快速 52毫秒就完成了！    证明是try是影响性能，当然是这是废话来的！  但有没外界传的那么影响性能呢，大家只看数据 相差10毫秒\r\n\r\n----------------------------------------------------------------------------------\r\n第四段代码，我们用 IF语句\r\n![4](https://cloud.githubusercontent.com/assets/16284765/12007348/e426572a-ac3a-11e5-88b3-8ebdf07ae75a.png)\r\n\r\n好了，看图片！一句简单的IF语句时，对性能影响不是太大，\r\n---------------------------------------------------------------------------------------------\r\n第五段代码，来个稍为有点压力的IF语句试试！\r\n\r\n![5](https://cloud.githubusercontent.com/assets/16284765/12007351/f4fcc9e4-ac3a-11e5-9f9d-d7fcf58a41e5.png)\r\n\r\n不会吧！3000多毫秒，足足输try一条街！不用伤心，起码比正则（简单正则）要快！\r\n\r\n------------------------------------------------------------------\r\n总结：\r\n好了，我现在我来总结一下！各位兄弟姐妹，如果对结果有意见自己亲自去测！不想讲太多废话了！\r\n\r\n有些人用那SUXXX，忘了什么测试插件啦，他说测出来的结果是try比还要正则要慢，这个人是群里，几乎要和我打架了，他说：所以我们应该用正则判断，不要用try来省事！\r\n\r\n但事实又是不是这样，我已经有一个模似真实环境的测试结果了，try的性能又是不是外界传的那恶劣呢！\r\n到底什么时候用 if,  什么时候用try来省事  ,  什么时候用正则，   大家看着我测试的结果来衡量了  ！再见   "},"comment":{"url":"https://api.github.com/repos/drduan/minggeJS/issues/comments/167595610","html_url":"https://github.com/drduan/minggeJS/issues/201#issuecomment-167595610","issue_url":"https://api.github.com/repos/drduan/minggeJS/issues/201","id":167595610,"user":{"login":"Cat7373","id":9296576,"avatar_url":"https://avatars.githubusercontent.com/u/9296576?v=3","gravatar_id":"","url":"https://api.github.com/users/Cat7373","html_url":"https://github.com/Cat7373","followers_url":"https://api.github.com/users/Cat7373/followers","following_url":"https://api.github.com/users/Cat7373/following{/other_user}","gists_url":"https://api.github.com/users/Cat7373/gists{/gist_id}","starred_url":"https://api.github.com/users/Cat7373/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Cat7373/subscriptions","organizations_url":"https://api.github.com/users/Cat7373/orgs","repos_url":"https://api.github.com/users/Cat7373/repos","events_url":"https://api.github.com/users/Cat7373/events{/privacy}","received_events_url":"https://api.github.com/users/Cat7373/received_events","type":"User","site_admin":false},"created_at":"2015-12-28T16:07:33Z","updated_at":"2015-12-28T16:07:33Z","body":"很抱歉我来打击你一下\r\nb = i++;\r\n这一句代码就让你的循环次数少了一半"}}