sleighty
sleighty

Reputation: 1075

What is faster: '-isKindOfClass:' or '-isEqualToString:'?

I'm making a game in SpriteKit and want to know whether the touched node is a specific one I'm looking for. Which would be faster, giving all nodes of that custom class a NSString name and using -isEqualToString: to check it or using -isKindOfClass: to check for the specific class?

Upvotes: 4

Views: 541

Answers (2)

Jef
Jef

Reputation: 4728

Class comparisons will almost certainly be the cheaper/more efficient of these two options. String comparison is expensive because strings need flexibility in their size (also, NSString is a cluster, the same string might be represented with an alternate data representation depending on how it was initialised) where 'class' is a known type of fixed memory footprint.

Upvotes: 5

Nixt
Nixt

Reputation: 144

While I have never used SpriteKit, from a game development/performance standpoint string compares are very inefficient. A faster way is to instead store a hashed string and compare against that, which is just an integer compare.

I hope this helps in deciding what is best for you.

Upvotes: 1

Related Questions