0

Hi,
:)
I am a newbie here and this is my first post.
:?:
I always wonder whether there is a better way of putting this or that or is there a work around for that, etc....
:idea:
So, its better if we can have an uncluttered discussion topic that will discuss all similar things under one roof...
:$
Expecting a great response and enlightening discussion from all of you... Lets begin ... :)

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by LaxLoafer
0

Hi,

Currently, in JavaScript, I append string to an existing string using the following method :

var str = "Hello ";
str += "fellow ";
str += "Daniweb.com ";
str += "members.";

I am looking out for a better and efficient way to do it ... please let me know...

Thanks.

0

I'll bite, you could create a function to append text... alas this would save you no lines of coding so I would say that depending on what you are trying to do, you sometimes need to tackle bigger problems with bigger ideas.

<script type="text/javascript">
function appendstr(orig,addto) {
	var str = orig;
	var add = addto;
	str = str + " " + add;
	return str;
// alternatively you can skip all code above and write this function in one line
        return orig + " " + addto;
// of course it really just depends on what you are trying to accomplish and I have added no type checking, you could even validate that both items are strings etc...
}

var str = "Hello";
str = appendstr(str,"fellow");
str = appendstr(str,"Daniweb.com");
str = appendstr(str,"members");
document.write(str);
</script>

Again, there is usually more that one way to do things, some methods or ways to write code are not necessarily wrong but not as efficient as they could be.
I also read about an implementation of a js StringBuffer, which I dealt with years ago in java. take a look at this code, I did not write it:

function StringBuffer() { 
   this.buffer = []; 
 } 

 StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
 }; 

 StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
 }; 

 var buf = new StringBuffer();
 buf.append("Hello");
 buf.append("fellow");
 buf.append("Daniweb.com");
 buf.append("members.");
 alert(buf.toString());

Again, I'll go back to point 1, right here are 3 different ways to append data onto a string. I think the stringbuffer approach comes at a performance cost, more-so than the other two methods (a function or just using +=).
It really boils down to what you are trying to do, what kind of data you are working with and how long it is taking to do it (execution time).

0

Thanks ddymacek,

in that case i think the first (the one that i wrote) is better ... at least that wont involve an overload of function calls ...

in .NET i believe, when we do string appending using the above method every time a new variable is created and then the string gets appended and that calls for an unwanted memory loss ... instead we use a class called StringBuilder

i was thinking that it is a similar case with javascript ...

thanks and i appreciate your efforts ...

0

Hi,

Currently, in JavaScript, I append string to an existing string using the following method :

var str = "Hello ";
str += "fellow ";
str += "Daniweb.com ";
str += "members.";

I am looking out for a better and efficient way to do it ... please let me know...

Thanks.

Rahul,

Your example makes use of an assignment operator. Because JavaScript strings are immutable, each assignment might result in the creation of a new string object.

You could also have written your example like this...

var str = "Hello " +
"fellow " +
"Daniweb.com " +
"members.";

The code above uses JavaScript's concatenation operator '+'.

However I doubt this would really make any difference in terms of performance. If there is a faster way of concatenating string literals, surely a compiler would optimize this sort of thing out? Using '+=' is a very common pattern for building strings ;-)

There are other ways to build strings. If you wish to delve further into the subject, here's a blog article that may interest you: String Performance: an Analysis, by Tom Trenka
http://www.sitepen.com/blog/2008/05/09/string-performance-an-analysis/

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.