wiki:EfficientStrings

Version 6 (modified by kenneth@webkit.org, 8 years ago) (diff)

Some more hints

Construction

Null and empty

Passing a null string is very efficient. Whenever possible, use a null String instead of an empty String.

From literal

Each String type has a efficient constructor to initialize the string from a literal:

  • String foo = ASCIILiteral("bar");
  • String foo("bar", String::ConstructFromLiteral);
  • AtomicString foo("bar", AtomicString::ConstructFromLiteral);

The safest option is ASCIILiteral("Foo"). It produces the same code size as String("Foo") while being faster.

The difference between the version is if the length of the string is included or not. Having the size given in the constructor makes the constructor faster. Having the size also makes the code bigger, which is a problem when the code is executed infrequently.

In general, use ASCIILiteral unless you can show improvement on a benchmark by using ConstructFromLiteral.

Not creating a string

Many operations can be more efficient with a literal. Do not create a String when it is not needed.

E.g.:

  • foo.startsWith("bar")
  • foo.startsWith(ASCIILiteral("bar"))
  • foo.startsWith(String("bar"))

The first version is the fastest.

Concatenation

There are two efficient way to concatenate strings: StringBuilder and StringOperators. Anything else is pretty much less efficient when doing more than one operations.

E.g.:

str = text;
str.append("a"); // == str.append(String("a"));
str.append(foo);
str += bar;

Should be (StringOperators):

str = text + 'a' + foo + bar;

Note the use of 'a' here instead of "a" as it is more efficient.


E.g:

str = foo;
for (size_t i = 0; i < foobars; ++i) {
   str += "bar";

should be:

StringBuilder builder;
builder.append(foo);
for (size_t i = 0; i < foobars; ++i) {
   builder.appendLiteral("bar");
str = builder.toString();

Note: If you need to append a literal char, builder.append('c'); is more efficient than builder.appendLiteral("c");