p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080

Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 26th, 2009, 04:24 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 2%
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 3 - StringBuffer vs += Performance Issue

I have just copied down the code for the StringBuffer "class" performance comparison on pgs 97-98 of the book but my results are opposite to what the book is explaining. The += operation without using the StringBuffer class is actually running significantly faster. Here is the code:

Code:
function StringBuffer1() {
	this._strings = new Array;

	if (typeof StringBuffer1._initialized == "undefined") {
		StringBuffer1.prototype.append = function (str) {
			this._strings.push(str)
		};
		StringBuffer1.prototype.toString = function () {
			return this._strings.join("");
		};
		StringBuffer1._initialized = true;
	};
};

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




var d1 = new Date();
var str = "";
for (var i=0; i < 10000; i++) {
	str += "text";
}
var d2 = new Date();
document.write("Concatenation with plus: " + (d2.getTime() - d1.getTime()) + " milliseconds<br />");

var oBuffer = new StringBuffer1();
d1 = new Date();
for (var i=0; i < 10000; i++) {
	oBuffer.append("text");
}
var sResult = oBuffer.toString();
d2 = new Date();
document.write("Concatenation with StringBuffer1: " + (d2.getTime() - d1.getTime()) + " milliseconds<br />");

var oBuffer = new StringBuffer2();
d1 = new Date();
for (var i=0; i < 10000; i++) {
	oBuffer.append("text");
}
var sResult = oBuffer.toString();
d2 = new Date();
document.write("Concatenation with StringBuffer2: " + (d2.getTime() - d1.getTime()) + " milliseconds<br />");
results:
Concatenation with plus: 12 milliseconds
Concatenation with StringBuffer1: 23 milliseconds
Concatenation with StringBuffer2: 26 milliseconds

I do have the 2005 version of the text, so I don't know if something has been changed with javascript since this publication. I'm also using Firefox 3.0.14 right now.

Last edited by IAmCorbin : October 26th, 2009 at 04:27 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
performance issue keyvanjan Classic ASP Basics 0 May 23rd, 2006 11:57 AM
Performance Issue of Execution bjohnjebastin ASP.NET 1.0 and 1.1 Basics 1 November 14th, 2005 11:18 AM
Transactions and performance issue avanishp ASP.NET 1.1 0 August 27th, 2005 08:59 AM
issue of performance alyeng2000 SQL Server 2000 6 August 20th, 2004 02:17 PM
Performance issue deyakhatib SQL Server 2000 2 June 21st, 2004 11:47 PM



All times are GMT -4. The time now is 02:16 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc