一直很迷惑司徒正美为什么对以下的for循环还鄙视,
for (var i = 0; i < list.length; i++) {
//循环代码
}
看了下面这篇文章终于TM明白了。。。
在JavaScript中,我们可以使用for(;;),while(),for(in)三种循环,事实上,这三种循环中for(in)的效率极差,因为他需要查询散列键,只要可以就应该尽量少用。
如果要与数组的长度作比较,应该事先把数组的length属性放入一个局部变量中,减少查询次数。
所以上面的代码应该这么写:
for (var i = 0,l = list.length; i < l; i++) {
//循环代码
}
局部变量的速度要比全局变量的访问速度更快,因为全局变量其实是全局对象的成员,而局部变量是放在函数的栈当中的。
如果是收集字符串,比如多次对同一个字符串进行+=操作的话,最好使用一个缓存。怎么用呢?使用JavaScript数组来收集,最后使用join方法连接起来,如下
var buf = new Array();
for (var i = 0; i < 100; i++) {
buf.push(i.toString());
}
var all = buf.join("");
类型转换是大家常犯的错误,因为JavaScript是动态类型语言,你不能指定变量的类型。
1. 把数字转换成字符串,应用"" + 1,虽然看起来比较丑一点,但事实上这个效率是最高的,性能上来说:("" +) > String() > .toString() > new String()
String()属于内部函数,所以速度很快,而.toString()要查询原型中的函数,所以速度逊色一些,new String()用于返回一个精确的副本。
2. 浮点数转换成整型,这个更容易出错,很多人喜欢使用parseInt(),其实parseInt()是用于将字符串转换成数字,而不是浮点数和整型之间的转换,我们应该使用Math.floor()或者Math.round()。另外,和第二节的对象查找中的问题不一样,Math是内部对象,所以Math.floor()其实并没有多少查询方法和调用的时间,速度是最快的。
3. 对于自定义的对象,如果定义了toString()方法来进行类型转换的话,推荐显式调用toString(),因为内部的操作在尝试所有可能性之后,会尝试对象的toString()方法尝试能否转化为String,所以直接调用这个方法效率会更高
其实这个影响倒比较小,可以忽略。什么叫使用直接量,比如,JavaScript支持使用[param,param,param,...]来直接表达一个数组,以往我们都使用new Array(param,param,...),使用前者是引擎直接解释的,后者要调用一个Array内部构造器,所以要略微快一点点。
同样,var foo = {}的方式也比var foo = new Object();快,
var reg = /../;要比var reg=new RegExp()快。
对字符串进行循环操作,譬如替换、查找,应使用正则表达式,因为本身JavaScript的循环速度就比较慢,而正则表达式的操作是用C写成的语言的API,性能很好。
自定义高级对象和Date、RegExp对象在构造时都会消耗大量时间。如果可以复用,应采用缓存的方式。
很多人喜欢在JavaScript中使用document.write来给页面生成内容。事实上这样的效率较低,如果需要直接插入HTML,可以找一个容器元素,比如指定一个div或者span,并设置他们的innerHTML来将自己的HTML代码插入到页面中。
使用[“”]查询要比.items()更快,这和前面的减少对象查找的思路是一样的,调用.items()增加了一次查询和函数的调用。
通常我们可能会使用字符串直接写HTML来创建节点,其实这样做无法保证代码的有效性。
So you should use the document.createElement() method, and if there are ready-made template nodes in the document, you should use the cloneNode() method, because after using the createElement() method, you need to set multiple elements. Attributes, using cloneNode() can reduce the number of attribute settings - similarly, if you need to create many elements, you should prepare a template node first.
If you are targeting code that is constantly running, you should not use setTimeout, but setInterval. setTimeout resets a timer each time.
Microsoft's JScript is much less efficient than Mozilla's Spidermonkey, both in terms of execution speed and memory management, because JScript is basically not updated now. But SpiderMonkey cannot use ActiveXObject
File optimization is also a very effective method. Delete all spaces and comments and put the code in one line to speed up the download. Note that it is the download speed and not the parsing speed. If it is local , comments and spaces do not affect interpretation and execution speed.
Some methods to improve JavaScript running performance in JavaScript programming. In fact, these experiences are based on several principles:
1. It is faster to directly use ready-made things at hand. For example, local variables are faster than global variables, direct variables are faster than constructing objects at runtime, etc.
2. Reduce the number of executions as much as possible, such as caching those that require multiple queries first.
3. Use built-in language functions as much as possible, such as string links.
4. Use the APIs provided by the system as much as possible, because these APIs are compiled binary codes and have high execution efficiency
5. Some basic algorithm optimizations can also be used in JavaScript, such as adjustments to the operation structure. However, since JavaScript is interpreted, bytecodes are generally not optimized at runtime, so these optimizations are still very important.
Of course, some of the techniques here are also used in other interpreted languages, and you can also refer to them.