I read from a programming book about 7-8 years ago that checking string.length == 0 is a faster way to check for empty strings. I’m wondering if that statement still holds true today (or if it has ever been true at all), because I personally think string == "" is more straightforward and more readable. I mostly deal with high-level languages such as .NET and java.
I read from a programming book about 7-8 years ago that checking string.length ==
Share
Usually, string object store their length and therefore getting and comparing the integer is very fast and has less memory access than an equals() where you – in the worst case – have to check the length and loop over the characters.
Anyway, nowadays the equals() method of a string should also check for the length first and therefore it should be – nearly – the same speed as checking for the length.
equals part in Java (http://www.docjar.com/html/api/java/lang/String.java.html):
equals part in Objective-C (http://www.opensource.apple.com/source/CF/CF-476.15/CFString.c) – NSString is based on CFString: