Ad
  • Custom User Avatar
  • Custom User Avatar

    @jhoffner : No, the result will be exactly the same : " World" in both cases.

  • Custom User Avatar

    Of course, in this instance the array being used changes the way the function handles edge cases as well. I would argue that is much more relevant then the microscopic performance benefit.

    With the array, passing in an empty string for one of the params will not result in a string being returned that would need to be trimmed.

    i.e. say('')('World') would return "World" instead of " World".

  • Custom User Avatar

    Hey laoris, in the same post he warns against building a webpage using +=, but I suppose it's for maintainability reasons rather than computation costs.

  • Default User Avatar

    I don't see Jeff Atwood say that you "shouldn't use string concatenation ever" in that article. In fact it would seem that he is arguing the opposite. The article investigates the best way to do small bits of string concatenation (e.g., not in a loop), and his last section is headlined "It. Just. Doesn't. Matter!".

    His conclusion is that your primary concern should be to write better code: "you should be more worried about the maintainability and readability of your code than its performance". Furthermore he warns that "Any time you see 'don't ever do X', alarm bells should be going off".

    Joining an array is a fairly well-known JavaScript idiom to concatenate strings, but it still carries a higher cognitive load that simply using the + operator, which is much simpler and explicit. Therefore in this case, where we are concatenating just three strings, I'd argue that the best practice would be just add the strings together.

    That said, I would also be interested to know node.js can optimize string1 + ' ' + string2 into a single memory allocation or if it would do (string1 + ' ') + string2.

  • Custom User Avatar

    A lot of people subscribe to Jeff Atwood's position that you should never use string concatenation, ever.

    http://blog.codinghorror.com/the-sad-tragedy-of-micro-optimization-theater/

    It would be nice to do a side by side comparison of the disassembled JIT code that node.js generates for this and the string concatenation solution look like.