Bug 188882 - String.concat() significantly slower than '+' operator
Summary: String.concat() significantly slower than '+' operator
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 11
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-08-23 04:23 PDT by Nicholas Shanks
Modified: 2018-08-27 02:12 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nicholas Shanks 2018-08-23 04:23:27 PDT
When String.prototype.concat() and String.prototype.join() have not been modified from their initial values, promotion of their arguments to strings and concatenation of those strings should be nearly as fast as the '+' operator with one string operand (which also has to promote the other operand). This will allow functional composition to be performant.

See https://jsperf.com/concat-vs-plus-vs-join-2
Comment 1 Radar WebKit Bug Importer 2018-08-23 09:46:38 PDT
<rdar://problem/43646782>
Comment 2 Keith Miller 2018-08-27 02:12:12 PDT
It looks like you are seeing such a large difference because the test is passing more than one argument to concat at the same time. This causes us to emit a loop which is slowing that code down.

If you change your test to:

string1.concat(string2).concat(string3);

you will see that the perf is quite close. In theory we could do some loop unrolling to fix this but the same issue applies to calling the '+' operator in a loop.

The join case is slow because it will allocate an Array, which is expensive. We could also optimize this but it's probably easier to use the concat method for now.