+ 2

How efficient is adding strings?

I want my program to add multiple characters (or small strings) into a storage string. Will it be efficient? My program is gonna be something like this: var storage = ""; for (var i = 0; i < 100000; i++) { storage = storage + "a"; }

29th May 2020, 6:37 PM
An Tran
29 Antworten
+ 5
I'd recommend running several tests with different input sizes. Some algorithms are able to operate efficiently on small data sets, but don't scale well. Sidenote: I'm not sure how JS concatenates strings internally, but it'd probably be better if you concatenated the string in chunks. For example, instead of adding 1 "a" 100,000 times, you add 10 "a"s 10,000 times.
29th May 2020, 8:28 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 5
Serialization is the process of turning an object into a linear form. In my JS object example, a JS object isn't linear. It can have several different branches, nested objects, etc. Serializing it would mean to convert the JS object into JSON. JSON is considered linear because it's a continuous chunk of plain text. It is now in a format that can be easily transported.
30th May 2020, 3:38 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 2
Can I ask why you're doing this?
29th May 2020, 8:29 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 2
An Tran Well JS is garbage collected, and I've never run into a memory leak, so while I can't say for sure, I'd imagine it handles string allocation/deallocation just fine. I will keep researching this though. EDIT: References don't need to be kept track of, they're allocated on the stack, so when they go out of scope they're automatically deleted. When an object has no more reachable references, it get deleted. So JavaScript doesn't have to find and delete every single reference. It just has to wait until an object has no more references, then delete it.
4th Jun 2020, 2:37 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 2
An Tran A computer program manages variables with a data structure called a stack. When a function is called, all the variables are allocated on the stack. When the function exits, all those variables are overwritten by the next stack frame. So this management is done automatically by the CPU. Nothing needs to be kept track of - It's completely passive. So once an objects references (on the stack) get overwritten, they're no longer reachable. Hope that makes sense. It's a very high level overview. If this type of stuff interests you then I'd recommend researching how a program's memory is laid out (stack, heap, data section, code section, etc.)
5th Jun 2020, 12:55 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 1
The only way to find out is to test it.
29th May 2020, 7:10 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 1
It's a simple shooting game, which calculates stuff each frame based on whether you are pressing the button or not. I will record the action of press and release of the button and then write it to that string. Normally for real life project, we would need to write to a file, but at the moment i want it to be easily shareable in SoloLearn.
30th May 2020, 2:27 AM
An Tran
+ 1
Ohhh, you want to share it. Do you plan on parsing the data in anyway after it's been shared? If so, you might want to create a json object, serialize it, then convert it to base64, then on the other end do the reverse until it's back to a json object, and it'll be much easier to parse
30th May 2020, 3:05 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 1
An Tran Well JS will technically create a new string, but it'll also unallocate the memory for the previous string. It should be fine.
30th May 2020, 3:20 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 1
An Tran And that's where the garbage collector comes in. It sees that the references are no longer valid (overwritten in the stack) and deletes the objects.
5th Jun 2020, 3:05 PM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 1
An Tran Sorry for the late reply, I started a summer internship and haven't had much time for SoloLearn. GC usually only runs periodically and will clean up unused memory. I'm not quite sure what you're referring to as "functions".
15th Jul 2020, 1:40 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
0
Yeah. Sure strings can be as long as they need to be.
29th May 2020, 8:05 PM
Ore
Ore - avatar
0
I saw a game (in here, SoloLearn) that is quite fun, so i want to add the recording feature for it. I just want something small, simple, and easy to share feature. I know that it won't fit for real life projects but i just want to have fun.
30th May 2020, 2:14 AM
An Tran
0
Can you elaborate on "Recording feature"? I'm trying to understand why you need to concatenate a large volume of strings for that?
30th May 2020, 2:18 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
0
Ah, I see. While you could write it to a string, appending those keypresses to an array would be a better way to approach it
30th May 2020, 2:38 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
0
That is the obvious way. But array of numbers is not as compact as string (and string is also an array of numbers), and it is hard to share it in normal text.
30th May 2020, 2:49 AM
An Tran
0
I was thinking about simple base64 only. Since the data is only an array of numbers, i'll just convert it to base64, not need for object or anything.
30th May 2020, 3:29 AM
An Tran
0
Ah, ok. I guess it doesn't really matter on such a small scale
30th May 2020, 3:33 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
0
What do you mean by "serialize it"?
30th May 2020, 3:33 AM
An Tran
0
Ahh, i get it.
30th May 2020, 3:46 AM
An Tran