js中(try catch) 對代碼的性能影響


https://blog.csdn.net/shmnh/article/details/52445186

 

起因

要捕獲 JavaScript 代碼中的異常一般會采用 try catch,不過 try catch 的使用是否是對代碼性能產生影響呢?答案是肯定有的,但是有多少不得而知。

淘寶前端線上腳本錯誤的捕獲方法:

 
window.JSTracker = window.JSTracker || []; try{  //your code }catch(e){  JSTracker.push(e);  throw e; //建議將錯誤再次拋出,避免測試無法發現異常 } 

設計實驗方式

簡單的設計方案也就是對比實驗。

空白組1:[無 try catch 的情況下對數據取模1千萬次耗時]

 
 <!DOCTYPE html> <html> <head>  <title>1 無try catch的情況耗時</title>  <script>  !function() {  //無try catch的情況耗時  var t = new Date();   //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  document.write(new Date() - t);  }();  </script> </head> <body>  </body> </html> 

參照組2:[將耗時代碼用 try 包圍,內聯耗時代碼]

 
<!DOCTYPE html>
<html> <head>  <title>2 在 try 中內聯代碼的耗時情況</title>  <script>  !function() {   //在 try 中內聯代碼的耗時情況  var t = new Date();  try{  //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  throw new Error();  }catch(e){   }  document.write(new Date() - t);  }();  </script> </head> <body>  </body> </html> 

參照組3:[將耗時代碼用 try 包圍,外聯耗時代碼]

 
<!DOCTYPE html>
<html> <head>  <title>3 在 try 中內聯代碼的耗時情況</title>  <script>  !function() {  function run(){  //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  }  //在 try 中內聯代碼的耗時情況  var t = new Date();  try{  run();  throw new Error();  }catch(e){   }  document.write(new Date() - t);  }();  </script> </head> <body>  </body> </html> 

參照組4:[將耗時代碼用 catch 包圍,內聯耗時代碼]

 
<!DOCTYPE html>
<html> <head>  <title>4 在 catch 中內聯代碼的耗時情況</title>  <script>  !function() {   //在 catch 中內聯代碼的耗時情況  var t = new Date();  try{  throw new Error();  }catch(e){  //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束   }  document.write(new Date() - t);  }();  </script> </head> <body>  </body> </html> 

參照組5:[將耗時代碼用 catch 包圍,外聯耗時代碼]

 
<!DOCTYPE html>
<html> <head>  <title>5 在 catch 中內聯代碼的耗時情況</title>  <script>  !function() {  function run(){  //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  }  //在 catch 中內聯代碼的耗時情況  var t = new Date();  try{  throw new Error();  }catch(e){  run();  }  document.write(new Date() - t);  }();  </script> </head> <body>  </body> </html> 

運行結果(只選取了 Chrome 作為示例)

- 不使用 try-catch try 中耗時,內聯代碼 try 中耗時,外聯代碼 catch 中耗時,內聯代碼 catch 中耗時,外聯代碼
Chrome44 98.2 1026.9 107.7 1028.5 105.9

給出總結

  • 使用 try catch 的使用無論是在 try 中的代碼還是在 catch 中的代碼性能消耗都是一樣的。
  • 需要注意的性能消耗在於 try catch 中不要直接塞進去太多的代碼(聲明太多的變量),最好是吧所有要執行的代碼放在另一個 function 中,通過調用這個 function 來執行。

針對第二點,可以查看 ECMA 中關於 try catch 的解釋,在代碼進入 try catch 的時候 js引擎會拷貝當前的詞法環境,拷貝的其實就是當前 scope 下的所有的變量。

建議

在使用 try catch 的時候盡量把 try catch 放在一個相對干凈的 scope 中,同時在 try catch 語句中也盡量保證足夠少的變量,最好通過函數調用方式來 try catch。

試驗中的現象解釋

測試過程中還是發現了一個疑問, 以下兩段代碼在 Chrome 44 中運行出來的結果差距非常大,加了一句空的 try catch 之后平均為:850ms,加上之前為:140ms。

 
!function() {  //無 try catch 的情況耗時  var t = new Date();   //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  document.write(new Date() - t);  try{  }catch(e){   }  }(); 
 
!function() {  //無 try catch 的情況耗時  var t = new Date();   //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  document.write(new Date() - t);   }(); 

其實原因很簡單
只要把代碼改為這樣 耗時就降下來了:

 
!function() {  !function() {  //無 try catch 的情況耗時  var t = new Date();   //耗時代碼開始  for (var i = 0; i < 100000000; i++) {  var p = i % 2;  }  //耗時代碼結束  document.write(new Date() - t);  }();  try{  }catch(e){   }  }();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM