What is the time complexity of string concatenation javascript?
I am learning algorithms. And I didn't find any articles about how to calculate Time Complexity of string concatenation in JavaScript. Show
We can concat strings using operator '+' or using built-in method String.prototype.concat Which method is better? What is the Time Complexity of both methods?
asked Sep 4, 2021 at 17:12
17
$\begingroup$ I was going through this piece of code from an algorithms books and something doesn't look clear Please ignore the spelling errors, How does 0(x + 2x + nx) reduce to o(xn^2) ? My analogy, assuming x is a constant 1 and n is 2 (x + 2x) == 3 assuming x is a constant 1 From the book (x2^2) == 4 assuming x is a constant 1 Am i right ? asked Feb 26, 2020 at 7:34
$\endgroup$ 1 $\begingroup$
answered Feb 26, 2020 at 8:38
anionanion 2161 silver badge3 bronze badges $\endgroup$ 2 $\begingroup$ The author did not say $$x+2x+\cdots+nx=n^2x.$$ He said $$x+2x+\cdots+nx=\frac{n(n+1)}2x$$ so that $$x+2x+\cdots+nx=O(n^2x).$$ If you don't know the meaning of the asymptotic notation $O$, read https://en.wikipedia.org/wiki/Big_O_notation. Notice that $1+2=\dfrac{2\cdot(2+1)}2$ is quite right. answered Aug 14 at 19:12
Yves DaoustYves Daoust 5,23512 silver badges30 bronze badges $\endgroup$ $\begingroup$ Here, Big $ O $ notation refers to the asymptotic upper bound to the running time as a function of input length. A formal definition and reference can be found here. As $ n^2 $ is a satisfactory upper bound to the function $ \frac{n(n+1)}{2} $ , the running time which comes out as $ \frac{n(n+1)}{2} $ can be expressed as $ O(n^2) $ answered Feb 26, 2020 at 8:59
SiluPandaSiluPanda 4993 silver badges10 bronze badges $\endgroup$ $\begingroup$ We can't say what the time complexity is, because it depends on the implementation. There is no good reason why making a copy of a string with n characters should take O(n), and not O(1). Actually, implementors of standard libraries and languages will work hard to make sure the time is O(1). I would be reasonably sure that equivalent code in Swift would run in O (xn). At least if we replace "sentence = sentence + w" with "sentence.append(w)", and a clever compiler can do that. answered Jul 25, 2020 at 12:27
gnasher729gnasher729 25.9k30 silver badges40 bronze badges $\endgroup$ Is string concatenation slow?Each time strcat calls, the loop will run from start to finish; the longer the string, the longer the loop runs. Until the string is extensive, the string addition takes place very heavy and slow.
Is concatenation faster than join?Doing N concatenations requires creating N new strings in the process. join() , on the other hand, only has to create a single string (the final result) and thus works much faster.
What is JavaScript string concatenation?In JavaScript, concat() is a string method that is used to concatenate strings together. The concat() method appends one or more string values to the calling string and then returns the concatenated result as a new string.
What is the best way to concatenate strings in JavaScript?The best and fastest way to concatenate strings in JavaScript is to use the + operator. You can also use the concat() method.
|