5

We unfortunately do a lot of dynamic web page design by building strings of HTML using JavaScript and using document.write to output the data. I stumbled upon some code that one of my coworkers wrote that looks like the following:

var myString = String() + "this is my string" +
            "and I am doing a lot of string concatenation" +
            "doing this the worst way possible"

These lines go on and on, sometimes hundreds of lines of hard-coded HTML (with inline styles and missing end tags). The part that I am curious about is the String() . I've never seen this used before, and I've been writing JavaScript for a long time. I asked my co-worker what it was and he said that "It improves the performance of the string concatenation and while stepping through during debugging, you won't step on each line, but rather straight to the end".

Now I usually take these things with a grain of salt, but it made me curious... so I tested it out. Chrome, at least, always steps to the next statement regardless of the opening String() or not. So I know that point to at least be untrue.

So A. what is is? Its not really a constructor (as it were), and when I type String() into the console I get back the empty string "". And B. Is there any truth to his statement that it improves performance? And if so, why?

4
  • 3
    It might affect a specific debugger, other than the one in Chrome. Remember, that's only one browser. That being said, he's had issues concatenating strings that require a debugger, I think there's bigger problems with the code than using String() instead of "". Commented Sep 14, 2015 at 20:49
  • Well they contain tons of variables so stepping through to check the variables as it concatenated and such. I know it can be done a whole lot better, but since we don't do it any other way I'm interested in this as it is Commented Sep 14, 2015 at 20:58
  • 1
    At least for chrome, firefox and IE11 the performance is abysmal: fiddle Commented Sep 14, 2015 at 21:40
  • This is unnecessary and could be removed. Commented Sep 15, 2015 at 8:30

1 Answer 1

2

From String - JavaScript | MDN:

The String global object is a constructor for strings, or a sequence of characters.

As to whether using it as in your example improves performance, there are probably two things to keep in mind:

  • Whether it improves performance is probably dependent on the browser or other JavaScript runtime environment.
  • It probably doesn't improve performance enough to warrant using it.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I've never seen the global string object used like that. And googling for String() turned up nothing as I would assume it's so common and I mistook the MDN page to be different from what I was looking for
@pinkfloydx33 Yeah, that's a weird usage – and generally stuff like that is suspect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.