---
schema_version: "1"
id: e66681cc75c7ae1bfbf94bdd337f6616b3d8352807491634f939eec661aceddf
title: "ApexGuru Antipattern: Expensive String Comparison"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/apexguru_antipattern_expensive_string_comparison.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_expensive_string_comparison.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 521c1fb6b58cef481b1b31e5f6d9ad462ba300e09e62dfb14a11ba60552792ba
description: The == operator for string comparison invokes java.text.Collator.compare(), which is slower than using String.equals() or String.equalsIgnoreCase(). Longer strings and larger loops increase overhead.
products:
  - Cross Cloud
release: "264"
---
# ApexGuru Antipattern: Expensive String Comparison

The == operator for string comparison invokes java.text.Collator.compare(), which is slower than using String.equals() or String.equalsIgnoreCase(). Longer strings and larger loops increase overhead.

## Scope

Detection and recommendation

## Detection Example

```java
apex
  if (content.respKey == key) {
      // Expensive string comparison
  }
```

## Recommendation

Use `String.equals()` for case-sensitive comparisons and `String.equalsIgnoreCase()` when ignoring capitalization. Avoid using `==` for a string comparison when locale-based comparison isn’t required. To prevent NullPointerException during comparison, add a null check.

```java
apex
    if (content.respKey != null && content.respKey.equals(key)) {
      // Optimized string comparison
  }
```