惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
S
Securelist
T
Tor Project blog
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 热门话题
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
博客园_首页
TaoSecurity Blog
TaoSecurity Blog
Attack and Defense Labs
Attack and Defense Labs
Project Zero
Project Zero
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
F
Full Disclosure
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
GbyAI
GbyAI
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
博客园 - 叶小钗
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - 雪美·考拉

创建自己的Web Helper 在WebMatrix中使用数据库 理解WebMatrix工作台(四) 理解WebMatrix工作台(三) 理解WebMatrix工作台(二) 理解WebMatrix工作台 使用模板创建站点 Web应用程序库 启动WebMatrix 建立你的第一个WebMatrix程序 WebMatrix的安装 Web栈介绍 模式的一次简单尝试 MS.SQL Server 2005安装问题追踪总结 [导入]Flash CS4 初用体验 给总理的信 [导入]国外Flash源码的汉化问题 [导入]Windows XP下搭建IIS+PHP+MySql+Zend Debugger [导入]ADOBE CREATIVE SUITE 4 中文全套 [导入]重构出来的“简单工厂模式” [导入]Visual Studio 2008 对 JQuery 的智能感知支持!
JavaScript中高效字符串拼接
雪美·考拉 · 2009-03-29 · via 博客园 - 雪美·考拉

就像在C#中一样,我们可以使用”+”来拼接字符串,而对于操作频繁、讲究效率的字符串拼接操作我们应该选择StringBuilder类。在JavaScript中是否也存在这个问题呢?答案是肯定的,虽然JavaScript并没有为我们提供一个内置的StringBuilder对象,但是我们可以自己来创建一个!至于效率到底能提高多少,让程序说话吧!

//---StringBuilder---
function StringBuilder(){
	this.__string__ = new Array();
}
StringBuilder.prototype.append = function(str){
	this.__string__.push(str);
}
StringBuilder.prototype.toString = function(){
	return this.__string__.join("");
}
 
var d1 = new Date();
var buffer = new StringBuilder();
for(var i = 1; i < 10000; i++){
	buffer.append("E3Card");
}
var strResult = buffer.toString();
var d2 = new Date();
 
document.write("StringBuilder用时:" + (d2.getTime() - d1.getTime()) + "<br/>");
 
//---+-----
var d3 = new Date();
var str = "";
for(var i = 1; i < 10000; i++){
	str += "E3Card";
}
var d4 = new Date();
document.write("+链接用时:" + (d4.getTime() - d3.getTime()) + "<br/>");

在我的机器上(Core2 3.0G/4GRAM),反复F5后得到一个,比较平稳的测试结果:

StringBuilder用时:32
+链接用时:1109

哇哦,30多倍,我没看错吧!

好了,赶紧自己做好一个Lib,以后都用这个StringBuilder吧:

function StringBuilder(){
	this.__string__ = new Array();
}
StringBuilder.prototype.append = function(str){
	this.__string__.push(str);
}
StringBuilder.prototype.toString = function(){
	return this.__string__.join("");
}

原文来自:http://www.e3card.cn/blog/?p=112