Unfortunately, we do a lot of dynamic web page design by creating HTML strings using JavaScript and using document.write to output the data. I came across some code that one of my colleagues wrote, which looks like this:
var myString = String() + "this is my string" + "and I am doing a lot of string concatenation" + "doing this the worst way possible"
These lines continue, sometimes hundreds of lines of hard-coded HTML (with built-in styles and missing tags). The part I'm curious about is String() . I have never seen this before, and I have been writing JavaScript for a long time. I asked my colleague what it was, and he said that "it improves the performance of string concatenation and, having switched during debugging, you will not step on each line, but rather right to the end."
Now I usually take these things with salt, but I was curious ... so I checked it. Chrome, at the very least, always takes the step to the next statement whether or not String() opened. Therefore, I know that this point is at least wrong.
So what is it? This is not a constructor (as it were), and when I enter String() in the console, I return an empty string "" . And B. Is there any truth in his statement that this improves performance? And if so, why?
source share