《High Performance JavaScript》的一些摘要3

Just as with other programming languages, the way that you factor your code and the
algorithm you choose affects the execution time of JavaScript. Unlike other
programming languages, JavaScript has a restricted set of resources from which to
draw, so optimization techniques are even more important.
• The for, while, and do-while loops all have similar performance characteristics,
and so no one loop type is significantly faster or slower than the others.
• Avoid the for-in loop unless you need to iterate over a number of unknown object
properties.
• The best ways to improve loop performance are to decrease the amount of work
done per iteration and decrease the number of loop iterations.
• Generally speaking, switch is always faster than if-else, but isn’t always the best
solution.
• Lookup tables are a faster alternative to multiple condition evaluation using ifelse
or switch.
• Browser call stack size limits the amount of recursion that JavaScript is allowed to
perform; stack overflow errors prevent the rest of the code from executing.
• If you run into a stack overflow error, change the method to an iterative algorithm
or make use of memoization to avoid work repetition.
The larger the amount of code being executed, the larger the performance gain realized
from using these strategies.


Array joining is slower than other methods of concatenation in most browsers, but this
is more than compensated for by the fact that it is the only efficient way to concatenate
lots of strings in IE7 and earlier.



Intensive string operations and incautiously crafted regexes can be major performance
obstructions, but the advice in this chapter helps you avoid common pitfalls.
?When concatenating numerous or large strings, array joining is the only method
with reasonable performance in IE7 and earlier.
?If you don抰 need to worry about IE7 and earlier, array joining is one of the slowest
ways to concatenate strings. Use simple + and += operators instead, and avoid unnecessary
intermediate strings.
?Backtracking is both a fundamental component of regex matching and a frequent
source of regex inefficiency.
?Runaway backtracking can cause a regex that usually finds matches quickly to run
slowly or even crash your browser when applied to partially matching strings.
Techniques for avoiding this problem include making adjacent tokens mutually
exclusive, avoiding nested quantifiers that allow matching the same part of a string
more than one way, and eliminating needless backtracking by repurposing the
atomic nature of lookahead.
?A variety of techniques exist for improving regex efficiency by helping regexes find
matches faster and spend less time considering nonmatching positions .
?Regexes are not always the best tool for the job, especially when you are merely
searching for literal strings.
?Although there are many ways to trim a string, using two simple regexes (one to
remove leading whitespace and another for trailing whitespace) offers a good mix
of brevity and cross-browser efficiency with varying string contents and lengths.
Looping from the end of the string in search of the first nonwhitespace characters,
or combining this technique with regexes in a hybrid approach, offers a good alternative
that is less affected by overall string length.

http://blog.csdn.net/zaifendou/article/details/5746988
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值