Version 2 (modified by, 8 years ago) (diff)

Remove extraneous parenthesis


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.

Not creating a string

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


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

The first version is the fastest.


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


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

Should be (StringOperators):

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


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

should be:

StringBuilder builder;
for (size_t i = 0; i < foobars; ++i) {
str = builder.toString();