According to the documentation, the difference between using + and concat() is that contat() will convert the argument directly to a string while the + operator will convert the operands to a primitive value and then into a string. Summarizing, it will not make a significant difference.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat#description


The time complexity of concat operations in another language like Java is usually O(n^2), and is a common practice to use a StringBuilder to make it O(n). In JavaScript though, there is an optimized implementation of concatenation based on the rope data structure, making the time complexity O(log n) in most cases.

https://josephmate.github.io/java/javascript/stringbuilder/2020/07/27/javascript-does-not-need-stringbuilder.html

Answer from Artur Luis on Stack Overflow
Discussions

Javascript - Time and space complexity of splice and concat inside loop - Stack Overflow
These functions are optimized at ... application in a given scenario, then sure, considering their time-complexity would be paramount. ... @CalebLucas you are right split() ,concat() etc have been optimize by JS engine.... More on stackoverflow.com
🌐 stackoverflow.com
December 4, 2018
Time and space complexity of string iteration and concatenation in javascript - Stack Overflow
On this question I'm not really concerned so much with the correct answer, but rather, the explanation, since I have seen conflicting answers on the internet for this one. Here is an example prob... More on stackoverflow.com
🌐 stackoverflow.com
March 17, 2020
JavaScript Array.push is 945x faster than Array.concat
push() mutates an array in-place, and concat() completely duplicates the existing array and adds another array to it · Edit: grammar and copy paste failures from my console More on news.ycombinator.com
🌐 news.ycombinator.com
104
132
May 25, 2019
javascript - Why is + so bad for concatenation?
Everybody keeps saying that one of JavaScript's problems is using + [example] for string concatenation. Some say the problem is not using +, it's type coercion [see the comments from the previous e... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
July 6, 2011
🌐
freeCodeCamp
forum.freecodecamp.org › t › big-o-complexity-of-concat-vs-push-apply-inside-loops › 33513
Big-O Complexity of concat() vs push.apply() inside Loops
September 2, 2016 - I am studying Big-O Complexity and I am analyzing common JavaScript functions and algorithms to get a good grasp on the topic. Today, I am analyzing two ways of merging a large number of arrays inspired by the Steamrolle…
🌐
AlgoCademy
algocademy.com › link
Time Complexity Practice 2 in JavaScript | AlgoCademy
A naive solution would involve using the slice method to create subarrays and the concat method to merge them. However, this approach is not optimal because both slice and concat have a time complexity of O(n), where n is the length of the array.
🌐
SitePoint
sitepoint.com › blog › javascript › high-performance string concatenation in javascript
High-performance String Concatenation in JavaScript — SitePoint
November 5, 2024 - Safari 5.0.1: bizarrely, a standard append takes no more than 5ms but an array join is more than ten times slower at 55ms. The latest JavaScript engines are optimized for string concatenation operators.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Merge Arrays in JavaScript: With and Without Duplicates | Envato Tuts+
February 19, 2023 - On the other hand, the spread operator relies on the common iteration protocols. I tested some large arrays on different browsers to compare the spread operator and concat: The overall time complexity of the concat function is O(N).
Top answer
1 of 1
3

Normally a question like this is quite difficult to give a definite answer to, because different implementations of Javascript have different time complexities for basic array operations (such as creating a new array of size n). Javascript arrays will typically be implemented either as dynamic arrays or hashtables, and these data structures have different performance characteristics.

So, there is no definitive time complexity for splice to remove one element from an array. What we can say is that removing one element takes linear time for a dynamic array, and as @Ry- points out in the comments, also linear time for a hashtable, because of the need to renumber the later indices. We can also say that it's highly likely one of these two data structures is used, and no sensible implementation will take more than linear time to do splice.

Either way, the worst case for your algorithm is when x = 'aa...aa' and y = 'abb...bb', i.e. x is n copies of 'a', and y is 'a' followed by (m - 1) copies of 'b'.

For a dynamic array or a hashtable, then the time complexity for just the splice operations is O(nm²). This is because the outer loop iterates O(nm) times (note the i-- inside the loop, which happens every time the letter 'b' needs to be removed), and the splice operation requires shifting or renumbering O(m) elements in yArr after index i.

But suppose some more exotic data structure is used which supports removing an element in sub-linear time (e.g. a skip list). In that case, the above only gives O(nm) times the complexity of the "remove" operation. But we haven't counted concat yet; this creates a new data structure and copies every item into it, which will still take linear time. concat is called O(n) times and takes an average of O(n + m) time per call, so the complexity of just the concat operations is O(n² + nm).

So the time complexity is very likely O(n² + nm²), and certainly at least O(n² + nm); not linear.


The space complexity is O(n), since the length of yArr is never more than twice as long as xArr.

Find elsewhere
🌐
DEV Community
dev.to › uilicious › javascript-array-push-is-945x-faster-than-array-concat-1oki
Javascript Array.push is 945x faster than Array.concat 🤯🤔 - DEV Community
May 6, 2019 - It took six whole seconds to merge 15,000 arrays with an average size of 5 elements with .concat. What the hell is the Javascript's .concat method doing under the hood?. Tagged with showdev, javascript, webperf.
🌐
Zditect
zditect.com › blog › 51294099.html
string concatenation time complexity
We cannot provide a description for this page right now
🌐
Stack Overflow
stackoverflow.com › questions › 60729910 › time-and-space-complexity-of-string-iteration-and-concatenation-in-javascript
Time and space complexity of string iteration and concatenation in javascript - Stack Overflow
March 17, 2020 - My assumption #2 is that time complexity here is O(n). We look at each character once in the loop (that part is clear). Then we perform a concatenation. However, my assumption is that the cancatenation of 'a' + 'bcd' takes the same time as the concatenation of 'a' + 'bcdefghijklmn' ...
🌐
Hacker News
news.ycombinator.com › item
JavaScript Array.push is 945x faster than Array.concat | Hacker News
May 25, 2019 - push() mutates an array in-place, and concat() completely duplicates the existing array and adds another array to it · Edit: grammar and copy paste failures from my console
🌐
JavaScript in Plain English
javascript.plainenglish.io › understanding-time-and-space-complexity-of-common-javascript-built-in-methods-39a3285a6409
Understanding Time and Space Complexity of Common JavaScript Built-in Methods | by Kyle Le | JavaScript in Plain English
April 14, 2023 - Here are the time and space complexities of some common JavaScript built-in methods: Array.prototype.concat(): O(n) time complexity and O(n) space complexity. This method creates a new array that includes elements from the original array(s).
🌐
ITNEXT
itnext.io › decoding-big-o-notation-time-and-space-complexities-in-javascript-d31ddb8e1254
Decoding Big O Notation Time and Space Complexities in JavaScript | ITNEXT
August 3, 2023 - This concatenation operation takes linear time (O(n)), as it involves copying the elements into a new array. As a result, the time complexity of the quicksort algorithm is considered to be linearithmic (O(n log n)).
🌐
MSR
rajamsr.com › home › javascript string concatenation: 6 easy ways to do it
JavaScript String Concatenation: 6 Easy Ways To Do It | MSR - Web Dev Simplified
March 16, 2024 - You can join strings with other data types, without getting unexpected results because of the order or precedence of the operands. However, the concat() method also has some drawbacks, such as: It’s slower and less efficient than the plus operator. It needs to call a function and make a new string every time.
🌐
Jonlinnell
jonlinnell.co.uk › articles › spread-operator-performance
How slow is the Spread operator in JavaScript? | Jon Linnell
August 17, 2022 - Array.concat(), however, I would expect to do some lower level memory manipulation to duplicate and stack the arrays next to each other. This would explain the very slight increase in time-complexity with increased elements; the number of elements matters to an extent, but isn't as big a dent ...
🌐
Medium
medium.com › @chinweregina › object-array-time-and-space-complexity-in-javascript-00792792dfc5
Object/Array Time and Space Complexity in JavaScript | by Chinwe Regina | Medium
October 16, 2023 - Concat is used to combine two or more arrays into a new array. Moreover, as the number of inputs in each array that you are combining grows, so does the run-time in a linear fashion. Slice involves making a copy of an array and, also, has a run-time that grows in a linear fashion with inputs.
Top answer
1 of 4
73

Consider this piece of JavaScript code:

var a = 10;
var b = 20;
console.log('result is ' + a + b);

This will log

result is 1020

Which most likely is not what was intended, and can be a hard to track bug.

2 of 4
49

When you say "bad" do you mean "incorrect" or do you mean "slow"? The argument about using mathematical operators to do string concatenation is arguably an "incorrect" argument, but there's also an argument to be made that using + to do a lot of string concatenation can be very slow.

We're not talking about "Hello" + number when we talk about performance, we're talking about building up a relatively large string by repeatedly appending to it in a loop.

var combined = "";
for (var i = 0; i < 1000000; i++) {
    combined = combined + "hello ";
}

In JavaScript (and C# for that matter) strings are immutable. They can never be changed, only replaced with other strings. You're probably aware that combined + "hello " doesn't directly modify the combined variable - the operation creates a new string that is the result of concatenating the two strings together, but you must then assign that new string to the combined variable if you want it to be changed.

So what this loop is doing is creating a million different string objects, and throwing away 999,999 of them. Creating that many strings that are continually growing in size is not fast, and now the garbage collector has a lot of work to do to clean up after this.

C# has the exact same problem, which is solved in that environment by the StringBuilder class. In JavaScript, you'll get much better performance by building up an array of all the strings you want to concatenate, and then joining them together one time at the end, instead of a million times in the loop:

var parts = [];
for (var i = 0; i < 1000000; i++) {
    parts.push("hello");
}
var combined = parts.join(" ");
🌐
CodeBurst
codeburst.io › jsnoob-push-vs-concat-basics-and-performance-comparison-7a4b55242fa9
#JSNoob — push() vs concat() — Basics and Performance Comparison | by abhishek gupta | codeburst
September 23, 2017 - Clearly push() is better in performance than concat() . But if you are interested in merging more than two arrays then it would be advised to go for concat() . ... So I believe the first part of #JSNoob series will help a lot of devs who are either confused or unaware of the difference between push() and concat().
🌐
freeCodeCamp
forum.freecodecamp.org › t › which-is-more-efficient-str-some-string-or-array-push-join › 5802
Which is more efficient str += "some string" or array.push().join()
May 29, 2016 - I know String in JavaScript is immutable which means if I concatenate strings by using +=, a new string will be created with its content being copied from the old string, the old string then gets garbage collected, but …