Most Efficient Way to Concatenate Strings in JavaScript

Most efficient way to concatenate strings in JavaScript?

Seems based on benchmarks at JSPerf that using += is the fastest method, though not necessarily in every browser.

For building strings in the DOM, it seems to be better to concatenate the string first and then add to the DOM, rather then iteratively add it to the dom. You should benchmark your own case though.

(Thanks @zAlbee for correction)

Most efficient way to concatenate strings using spread arguments

I would suggest to go with using _.reduce.
The reason being said it uses += kind of logic around arrays of string to concatenate and this is perf efficient
jsperf

Snippet:

    _.reduce(['x', 'y', 'z'], function(accumulator, currentItem) {
return accumulator + currentItem;
});
// xyz

Reference:

https://jsperf.com/concat-join-reduce

Most efficient way to concat strings in javascript

Don't know if it's the fastest way, but what I find most readable (i.e. optimized for human eyes) is:

var str1 = "Hello ";
var str2 = "world!";
var res = [str1, str2].join('');
// output : "Hello world!"

Although usually only when we don't know ahead of time exactly what is going to be concatenated, so the sample looks really contrived.

How to build/concatenate strings in JavaScript?

With ES6, you can use

  • Template strings:

    var username = 'craig';
    console.log(`hello ${username}`);

ES5 and below:

  • use the + operator

    var username = 'craig';
    var joined = 'hello ' + username;
  • String's concat(..)

    var username = 'craig';
    var joined = 'hello '.concat(username);

Alternatively, use Array methods:

  • join(..):

    var username = 'craig';
    var joined = ['hello', username].join(' ');
  • Or even fancier, reduce(..) combined with any of the above:

    var a = ['hello', 'world', 'and', 'the', 'milky', 'way'];
    var b = a.reduce(function(pre, next) {
    return pre + ' ' + next;
    });
    console.log(b); // hello world and the milky way

Most efficient way to concatenate strings JavaScript in a for loop

I would use words.join(" || ")+" || " today is the fastest way, see this http://jsben.ch/DlXOa , as @jacob points out, it is a relative conclusion. But, as a general rule in JavaScript, usually built-in methods are faster.

What is the most efficient way to concatenate strings in Dart?

Here is what I understood:

It appears that performance will vary depending on your use case. If you use StringBuffer, and intend to concatenate a very large string, then as the concatenation occurs only when you call toString, it is at that point where you get the "performance hit".

But if you use "+", then each time you call "+" there is a performance hit. As strings are immutable, each time you concatenate two strings you are creating a new object which needs to be garbage collected.

Hence the answer seems to be to test for your situation, and determine which option makes sense.



Related Topics



Leave a reply



Submit