0

I did search this info on the web, some say yes because javascript must create a new string object to store the result of the concatenation, some say no because string objects are not collected.

Maybe it depends on the context. For example, if I had an array of objects like

animals["blue_dog","red_dog","yellow_cat","red_bird","green_bird"...]

and I had a function with animal and color arguments, in this function I would access my object like this:

animals[animal+"_"+color].

Most of the time I do concatenations when drawing text, which obviously doesn't happen a lot of times per frame. So even if it becomes garbage, it is insignificant. But when using a concatenation as an object's key, because of loops this concatenation could happen a thousand times per frame, and then this may become a problem.

SilverlightFox
  • 32,436
  • 11
  • 76
  • 145
Diego
  • 569
  • 1
  • 9
  • 28
  • Please provide some working code example. – zmii May 23 '15 at 21:22
  • Arrays are indexed with integers. `animals[animal+"_"+color]` is format for referencing object properties. – zmii May 23 '15 at 21:24
  • Even if this is a problem (it isn't), what's your alternative? – RobG May 23 '15 at 21:27
  • `var array = { "blue-dog": 1, "red-dog": 3, "red-fox": 2, "blue-fox": 4 } var heads, tails, i; heads = ["blue", "red"]; tails = ["dog", "fox"]; var CONST = 1000000; for( i = 0 ; i < CONST; i++ ) { array[heads[i%2] + '-' + tails[i%2]]; }` – zmii May 23 '15 at 21:31
  • Try this code with active Profiler in Chrome to check your theory. – zmii May 23 '15 at 21:32

3 Answers3

2

Doing something like animals[color + "_" + animal] creates a temporary value for accessing the object. That temporary will be collected either by the garbage collector or at the end of the block/function call (implementation dependent).

My assumption (based on my own experience in working with compilers) is that since the result isn't stored in a variable, it will be placed on the stack and released once the function completes. But, again, this depends on how the engine is written.

I'm in no way an expert on compilers.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
1

May or may not, depends on how optimal is the compiler.

This a + b + c is (a + b) + c so you have two concatenations. Result of (a + b) will be a temporary object (string here). That temporary is a garbage.

For the given expression this form a.concat(b,c) is conceptually better. It in principle does not require intermediate temporaries.

c-smile
  • 26,734
  • 7
  • 59
  • 86
1

In my experience, concatenating strings can definitely produce garbage. I had a scenario where I had a lot of cells in a large table and each cell could have a different css class (from a set of combinations of maybe 30 different combinations) assigned. Doing this the normal way:

const class = group + empty + active;

Would produce a lot of garbage. I created a memoized function that received a bitmask as argument which would produce the class string and got rid of the garbage that way.

This reply is an excellent list of what to avoid:

https://stackoverflow.com/a/18411275

rasmusvhansen
  • 1,452
  • 1
  • 12
  • 13