起因
要捕获 JavaScript 代码中的异常一般会采用 try catch,不过 try catch 的使用是否是对代码性能产生影响呢?答案是肯定有的,但是有多少不得而知。
前端线上脚本错误的捕获方法:
window . JSTracker = window . JSTracker || [ ] ;
try {
//your code
} catch ( e ) {
JSTracker . push ( e ) ;
throw e ; //建议将错误再次抛出,避免测试无法发现异常
}
设计实验方式
简单的设计方案也就是对比实验。
空白组1:[无 try catch 的情况下对数据取模1千万次耗时]
1无 try catch的情况耗时
<script></script>
! function ( ) {
//无try catch的情况耗时
var t = new Date ( ) ;
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
document . write ( new Date ( ) – t ) ;
} ( ) ;
参照组2:[将耗时代码用 try 包围,内联耗时代码]
2在 try中内联代码的耗时情况
<script></script>
! function ( ) {
//在 try 中内联代码的耗时情况
var t = new Date ( ) ;
try {
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
throw new Error ( ) ;
} catch ( e ) {
}
document . write ( new Date ( ) – t ) ;
} ( ) ;
参照组3:[将耗时代码用 try 包围,外联耗时代码]
3在 try中内联代码的耗时情况
<script></script>
! function ( ) {
function run ( ) {
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
}
//在 try 中内联代码的耗时情况
var t = new Date ( ) ;
try {
run ( ) ;
throw new Error ( ) ;
} catch ( e ) {
}
document . write ( new Date ( ) – t ) ;
} ( ) ;
参照组4:[将耗时代码用 catch 包围,内联耗时代码]
4在 catch中内联代码的耗时情况
<script></script>
! function ( ) {
//在 catch 中内联代码的耗时情况
var t = new Date ( ) ;
try {
throw new Error ( ) ;
} catch ( e ) {
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
}
document . write ( new Date ( ) – t ) ;
} ( ) ;
参照组5:[将耗时代码用 catch 包围,外联耗时代码]
5在 catch中内联代码的耗时情况
<script></script>
! function ( ) {
function run ( ) {
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
}
//在 catch 中内联代码的耗时情况
var t = new Date ( ) ;
try {
throw new Error ( ) ;
} catch ( e ) {
run ( ) ;
}
document . write ( new Date ( ) – t ) ;
} ( ) ;
运行结果(只选取了 Chrome 作为示例)
– | 不使用 try-catch | try 中耗时,内联代码 | try 中耗时,外联代码 | catch 中耗时,内联代码 | catch 中耗时,外联代码 |
---|---|---|---|---|---|
Chrome51 | 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
var p = i % 2 ;
}
//耗时代码结束
document . write ( new Date ( ) – t ) ;
try {
} catch ( e ) {
}
} ( ) ;
! function ( ) {
//无 try catch 的情况耗时
var t = new Date ( ) ;
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
document . write ( new Date ( ) – t ) ;
} ( ) ;
其实原因很简单
只要把代码改为这样 耗时就降下来了:
! function ( ) {
! function ( ) {
//无 try catch 的情况耗时
var t = new Date ( ) ;
//耗时代码开始
for ( var i = 0 ; i
var p = i % 2 ;
}
//耗时代码结束
document . write ( new Date ( ) – t ) ;
} ( ) ;
try {
} catch ( e ) {
}
} ( ) ;

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
