易截截图软件、单文件、免安装、纯绿色、仅160KB

javascript 优化


Optimizing JavaScript code
Authors: Gregory Baker, Software Engineer on GMail & Erik Arvidsson, Software Engineer on Google Chrome
Recommended experience: Working knowledge of JavaScript
Client-side
scripting can make your application dynamic and active, but the
browser's interpretation of this code can itself introduce
inefficiencies, and the performance of different constructs varies from
client to client. Here we discuss a few tips and best practices to
optimize your JavaScript code.
Working with strings
String
concatenation causes major problems with Internet Explorer 6 and 7
garbage collection performance. Although these issues have been
addressed in Internet Explorer 8 -- concatenating is actually slightly more
efficient on IE8 and other non-IE browsers such as Chrome -- if a
significant portion of your user population uses Internet Explorer 6 or
7, you should pay serious attention to the way you build your strings.
Consider this example:
var veryLongMessage =
'This is a long string that due to our strict line length limit of' +
maxCharsPerLine +
' characters per line must be wrapped. ' +
percentWhoDislike +
'% of engineers dislike this rule. The line length limit is for ' +
' style purposes, but we don't want it to have a performance impact.' +
' So the question is how should we do the wrapping?';
Instead of concatenation, try using a join:
var veryLongMessage =
['This is a long string that due to our strict line length limit of',
maxCharsPerLine,
' characters per line must be wrapped. ',
percentWhoDislike,
'% of engineers dislike this rule. The line length limit is for ',
' style purposes, but we don't want it to have a performance impact.',
' So the question is how should we do the wrapping?'
].join();
Similarly,
building up a string across conditional statements and/or loops by
using concatenation can be very inefficient. The wrong way:
var fibonacciStr = 'First 20 Fibonacci Numbers
';
for (var i = 0; i < 2


相关文档:

javascript 字符串处理全攻略


文章分类:
JavaScript
文章标题:
javascript 字符串处理全攻略
关  键 字:
0
文章作者:
alonglee
文章来源:
http://lmgq.vip.sina.com/tech/jsadvancedlesson/c2p1.htm

发表时间:
2004-9-21 14:43:00


一、声明字符串:
var normal_monkey = "I&n ......

Javascript中最常用的55个经典技巧【转载】

1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut="return f ......

Javascript两个小巧的自定义字符串格式化函数

document.close();
document.open();
function jsonFormat(template, json) {
return template.replace(/\$\{(.+?)\}/g, function ($, $1) {
return json[$1];
});
}
var links = [
{ text: "人肉搜索", url: "http://renrousousuo.com" } ,
{ text: "CSDN", url: &qu ......

JavaScript判断上传文件的大小

<form     name="form1">  
  <input type="file"     name="file1"     onchange="checkimage
(value)">  
  </form>  
  <script     language="javasc ......

IE与Firefox下javascript getyear年份的兼容性写法

                    前几天在测试网站兼容性问题,发现原来在IE下面正常的显示时间在Firefox获取年份下显示109
                &nb ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号