From the rhino book: In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings. Strings are assigned by reference, not by value. In general, when an object is assigned by reference, a change made to the object through one reference will be visible through all other references to the object.
Because strings cannot be changed, however, you can have multiple references to a string object and not worry that the string value will change without your knowing it.
5 Link to an appropriate section of the rhino book: books.google. Com/… – docgnome Jun 6 '09 at 5:55 Thanks, I didn't know that. – Braveyard Sep 8 '09 at 21:10.
They are immutable. However, I've always heard what Ash mentioned in his answer( that using Array. Join is faster for concatenation) so I wanted to test out the different methods of concatenating strings and abstracting the fastest way into a StringBuilder.
I wrote some tests to see if this is true. This was what I believed would be the fastest way, though I kept thinking that adding a method call may make it slower... function StringBuilder(/* str, str, str */) { this. _array = ; this.
_index = 0; this.append. Apply(this, arguments); } StringBuilder.prototype. Append = function (/* str, str, str, ... */ ) { for (var i=0; I Length; i++) { this.
_arraythis. _index = argumentsi; this. _index++; } } StringBuilder.prototype.
ToString = function () { return this. _array. Join(''); } So I decided to test them out... Here are some speed tests.
All three of them create a gigantic string made up of concatenating " diggity dog" one million times into an empty string. I tried each about five times and took some averages Array index http://jsbin.com/uhuqe4/edit Array push http://jsbin.com/ucusu3/edit String concat http://jsbin.com/opote3/edit Then I wrapped each style in a StringBuilder abstraction String Builder using Array index http://jsbin.com/ohecu3/5/edit String Builder using Array push http://jsbin.com/aracu6/edit String Builder using string concatenation http://jsbin.com/emayi5/2/edit Firefox 3.6.13 (150ms) Array index (275ms) Array push (555ms) String concat (669ms) StringBuilder: Array index (1420ms) StringBilder: Array push (1120ms) StringBuilder: String concat Chrome 8.0.552.237 (164ms) Array index (150ms) Array push (65ms) String concat (220ms) StringBuilder: Array index (240ms) StringBilder: Array push (211ms) StringBuilder: String concat IE 8.0.7600.16385 (685ms) Array index (1040ms) Array push (468ms) String concat (4100ms) StringBuilder: Array index (4200ms) StringBilder: Array push (3350ms) StringBuilder: String concat Findings Chrome is the fastest, and it's best at straight string concatenation.No need to trick it with Array. Join IE is fastest with straight string concatenation, it's really slow using the Array.
Join trick combined with Array.push. I got the long running script message in IE for all the StringBuilder tests. FF is best with Array.
Join with index access. Creating a StringBuilder to abstract away each browser's performance issues does more harm than good. The overhead of the extra method calls undoes anything gained by a specific method of concatenating strings.
Hope somebody else finds this useful Homework My StringBuilder abstractions use arguments in their function bodies; my tests show that functions that use the arguments (instead of named arguments) are much slower. If somebody wants to take the time to modify the tests without using arguments, we may find that a StringBuilder class is indeed feasible. Please post links to the new tests here, or just edit the answer to add the new results.
This is a great posting. Thanks for taking the time to code and measure these implementations. – stackoverflowuser2010 Jun 30 at 18:09.
Performance tip: If you have to concatenate large strings, put the string parts into an array and use the Array.Join() method to get the overall string. This can be many times faster for concatenating a large number of strings. No StringBuilder in javascript.
– docgnome Jun 6 '09 at 5:56 4 @docgnome: Because strings are immutable, string concatenation requires creating more objects than the Array. Join approach – Juan Mendes Jan 17 at 20:37.
I can't find any specifics on this. – DevelopingChris Sep 9 '08 at 3:50 "attempting to set an individual character won't work" -- developer.mozilla.Org/en/JavaScript/Reference/Global_Objects/… – Ken Feb 27 at 4:54.
Regarding your question (in your comment to Ash's response) about the StringBuilder in ASP. NET Ajax the experts seem to disagree on this one. Christian Wenz says in his book Programming ASP.NET AJAX (O'Reilly) that "this approach does not have any measurable effect on memory (in fact, the implementation seems to be a tick slower than the standard approach).
" On the other hand Gallo et al say in their book ASP. NET AJAX in Action (Manning) that "When the number of strings to concatenate is larger, the string builder becomes an essential object to avoid huge performance drops." I guess you'd need to do your own benchmarking and results might differ between browsers, too.
However, even if it doesn't improve performance it might still be considered "useful" for programmers who are used to coding with StringBuilders in languages like C# or Java.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.