Efficient JavaScript coding
1, 尽可能选择高效的method
e.g.
如果没有必要,可以不用regular expression
String.indexOf, String.lastIndexOf > String.match, String.search, String.replace
2, 面对large loop就要斤斤计较
2.1 Create once, use repeatedly
for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
if( oNode.nodeValue.match(/^\s*extra.*free/g) ) {
//this creates the expression
//it will be cached and re-used next time through the loop
}
}
for( var i = 0, oNode; oNode = oElement.childNodes[i]; i++ ) {
if( oNode.nodeValue.match(new RegExp(“^\s*extra.*free”,”g”)) ) {
//this will always create a new copy of the expression,
//and is generally much slower than creating static expressions.
}
}
2.2 Referencing element once, use repeatedly
var _table =$("#tableId")
for (var index in json) {
otherFun(_table, json[index]);
};
3 eval is evil
Eval 或者 new Function() 执行时,浏览器先创建整个scripting环境(就像一个新页面),导入scope chain中所有变量,执行script,gc, 最后导出所有变量到当前环境。(in a word, cost much)另外,js engine还不能对它们进行cache优化。
4 less is more
Less code, short naming
Only add event listener what you need
5 do less
Take a short circuit
e.g
var logger=window.console && window.console.dir
var logger=window.console || {}
less XHR calling
e.g. enable cache for the same request
6 Reduce reflow
每当添加element到document里,browser就会reflow整个页面去计算如何重新定位和渲染。
7,cache
Enable cache for duplicated XHR calling
Enable cache for js script file, so move out jscript from jsp to js file.
Reference:
http://slowjavascript.com/JavaScript_Performance_Rocks_Checklist.pdf
分享到:
相关推荐
Actionscript/Flex 3.0 Coding Convention 代码规范 v1.1 是一份由艺思哲软件(上海)有限公司在2008年发布的详细编程规范文档,旨在为ActionScript 3.0和Flex 3.0开发者提供一套统一的编码标准,提升代码质量和可读性...
在本话题中,我们将讨论如何在Sublime Text 2中安装和使用JavaScript代码格式化插件——Zen Coding。 Zen Coding,后来更名为Emmet,是前端开发者必备的工具之一,它提供了快速编写HTML和CSS的方法。通过简洁的缩写...
Apply design patterns to solve JavaScript coding problems Learn coding patterns that unleash the unique power of the language Write better and more maintainable JavaScript code Type in and play around...
通过以上分析,我们可以看出《Efficient Broadcasting Using Network Coding》这篇文章深入探讨了网络编码在提高无线网络广播能量效率方面的潜力,并通过理论分析和实际算法设计,展示了这一技术的实用性和有效性。
This book helps you answer that question with numerous JavaScript coding patterns and best practices. If you're an experienced developer looking to solve problems related to objects, functions, ...
This book is an exploration of popular advanced JavaScript concepts for those who already have a grasp on the basics....- Writing Fast, Memory-Efficient JavaScript - Designing Better JavaScript APIs
本文探讨了一种在高级语言中实现高效霍夫曼编码(Huffman coding)的变体方法。霍夫曼编码是一种广泛应用于数据压缩领域的算法,其核心优势在于能够为不同字符分配不同长度的编码,从而达到最佳压缩效果。然而,在...
Bruno A.Olshausen的经典稀疏编码matlab代码,文章是1996年LETTERS TO NATURE上的emergence of simple-cell receptive field properties by learning a sparse code for natural images.
在“Phase Coding”中,开发团队可能会使用各种编程语言和技术,如Java、Python、C++、JavaScript等,以及相应的开发框架和工具,如Spring Boot、Django、React或Vue.js等。同时,版本控制工具(如Git)和敏捷开发...
《SAP方丈-Coding Block详解》 在IT领域,SAP系统是企业资源规划(ERP)软件的领导者,广泛应用于全球各地的企业中。而Coding Block则是SAP ABAP编程中的一项重要概念,它对于理解SAP编程逻辑、提高代码效率和可...
What's the best approach for developing an application with JavaScript? This book helps you answer that question with numerous JavaScript coding patterns and best practices.
Coding Theory The Essentials
using JavaScript and p5.js and secondarily in creating visuals. The main focus is to teach you how to program so that you can choose to pursue whatever field that you would like with your newly ...
Pro JavaScript Development is a practical guide for front-end web developers who are ... Become a master of the latest JavaScript coding techniques and tools, and harness its best capabilities today.
Get Coding! Learn HTML, CSS & JavaScript & Build a Website, App & Game 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
javascript_coding 用编码修改JavaScript
【Coding4Fun控件】是专门为Windows Phone平台开发的一系列扩展控件库,它为开发者提供了许多超越原生Windows Phone SDK的功能和视觉效果,旨在增强应用程序的用户体验和交互性。这个库是由微软开发者社区成员创建并...
OpenCores是一个开源硬件项目社区,其提供的`opencores_coding_guidelines.pdf`文档详细介绍了针对VHDL和Verilog的编码规范。这些指导原则旨在确保代码的一致性和可读性,包括但不限于变量命名规则、注释标准、模块...