Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:35:56+00:00 2026-06-09T06:35:56+00:00

I have a tableview, with a search bar. The table is loaded with 3700

  • 0

I have a tableview, with a search bar. The table is loaded with 3700 text objects. The search occurs on the “title” text, which on average is 35 characters in length. I am looking for any optimization suggestions that would speed up the search sorting process. Currently, on average the search sort, is taking 0.733 seconds, which is about 95% of the entire search execution time. I am using a predicate that uses CONTAINS (has to, unfortunately) and then sortedArrayUsingComparator:, where I pass a block.

Thanks for taking a look!

Here is what I am doing:

    //My sorting block implementation
    self.mySortBlock = ^NSComparisonResult(id obj1, id obj2) {
        Tip *tip1 = obj1;
        Tip *tip2 = obj2;

        NSString *string1 = [tip1.subject lowercaseString];
        NSString *string2 = [tip2.subject lowercaseString];

        NSUInteger searchStringLocation1 = [string1 rangeOfString:[self.userSearchText lowercaseString]].location;
        NSUInteger searchStringLocation2 = [string2 rangeOfString:[self.userSearchText lowercaseString]].location;

        if (searchStringLocation1 > searchStringLocation2) return NSOrderedDescending;
        if (searchStringLocation1 < searchStringLocation2) return NSOrderedAscending;
        return NSOrderedSame;
    };

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

    NSPredicate *filter = [NSPredicate predicateWithFormat:@"subject CONTAINS [cd] %@", searchText];
    NSArray *filtered = [myArray filteredArrayUsingPredicate: filter];
    self.sorted = nil; 
    self.sorted = [filtered sortedArrayUsingComparator:self.mySortBlock];

}

EDIT
Per Catfish_Man’s suggestion below, I refactored the sort block to not instantiate objects inside the block unless absolutely necessary. I removed 4 object instantiations. The refactor brought with it, a +300% speed improvement:

enter image description here

Here is the refactored sort block:

self.mySortBlock = ^NSComparisonResult(id obj1, id obj2) {


    NSUInteger searchStringLocation1 = [[obj1 subject] rangeOfString:self.userSearchText options:NSCaseInsensitiveSearch].location;

    NSUInteger searchStringLocation2 = [[obj2 subject] rangeOfString:self.userSearchText options:NSCaseInsensitiveSearch].location;


     if (searchStringLocation1 > searchStringLocation2) return NSOrderedDescending;
     if (searchStringLocation1 < searchStringLocation2) return NSOrderedAscending;
     return NSOrderedSame;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:35:57+00:00Added an answer on June 9, 2026 at 6:35 am

    Biggest thing I see: you’re allocating 4 objects in your sort function. Object allocation just isn’t very fast! Instead, try using -rangeOfString:options: and passing NSCaseInsensitiveSearch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UI table view with a search bar and it works well
I have a table view with a search bar at the top of the
I have a tableview with a search bar. All is working fine but now
In my application there is a tableView & a search bar. I have an
I have a table view along with search bar. I want to ensure that
I have added search bar in my tableview and I am having number of
I have a table view set up with a search bar. Everything works great:
I have set up a search bar for my table view, and I would
I have one table view that should be filtered by values of search field
I have a search display controller in a table view. In this table view,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.