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 8666829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:49:35+00:00 2026-06-12T17:49:35+00:00

I have a PhoneGap mobile application that I need to generate an array of

  • 0

I have a PhoneGap mobile application that I need to generate an array of match combinations. In JavaScript side, the code hanged pretty soon when the array of which the combinations are generated from got a bit bigger. So, I thought I’ll make a plugin to generate the combinations, passing the array of javascript objects to native side and loop it there.

To my surprise the following codes executes in 150 ms (JavaScript) whereas in native side (Objective-C) it takes ~1000 ms.

Does anyone know any tips for speeding up those executing times? When players exceeds 10, i.e. the length of the array of teams equals 252 it really gets slow. Those execution times mentioned above are for 10 players / 252 teams.

Here’s the JavaScript code:

for (i = 0; i < GAME.teams.length; i += 1) {
            for (j = i + 1; j < GAME.teams.length; j += 1) {
                t1                  = GAME.teams[i];
                t2                  = GAME.teams[j];

                if ((t1.mask & t2.mask) === 0) {

                    GAME.matches.push({
                        Team1: t1,
                        Team2: t2
                    });
                }
            }
}

… and here’s the native code:

NSArray *teams = [[NSArray alloc] initWithArray: [options objectForKey:@"teams"]];
NSMutableArray *t = [[NSMutableArray alloc] init];
int mask_t1;
int mask_t2;

for (NSInteger i = 0; i < [teams count]; i++) {
        for (NSInteger j = i + 1; j < [teams count]; j++) {

            mask_t1     = [[[teams objectAtIndex:i] objectForKey:@"mask"] intValue];
            mask_t2     = [[[teams objectAtIndex:j] objectForKey:@"mask"] intValue];

            if ((mask_t1 & mask_t2) == 0) {
                [t insertObject:[teams objectAtIndex:i] atIndex:0];
                [t insertObject:[teams objectAtIndex:j] atIndex:1];
                /*
                NSArray *newCombination = [[NSArray alloc] initWithObjects:
                                           [teams objectAtIndex:i],
                                           [teams objectAtIndex:j],
                                           nil];
                */
                [combinations addObject:t];
            }
        }
}

… the array in question (GAME.teams) looks like this:

{
    count = 2;
    full = 1;
    list =         (
                    {
            index = 0;
            mask = 1;
            name = A;
            score = 0;
        },
                    {
            index = 1;
            mask = 2;
            name = B;
            score = 0;
        }
    );
    mask = 3;
    name = A;
},
    {
    count = 2;
    full = 1;
    list =         (
                    {
            index = 0;
            mask = 1;
            name = A;
            score = 0;
        },
                    {
            index = 2;
            mask = 4;
            name = C;
            score = 0;
        }
    );
    mask = 5;
    name = A;
},
  • 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-12T17:49:36+00:00Added an answer on June 12, 2026 at 5:49 pm

    Generally when you have a performance problem, you should profile your app using the Time Profiler instrument.

    In this case, I can see some likely problems.

    First of all, you’re doing this in your inner loop:

    [t insertObject:[teams objectAtIndex:i] atIndex:0];
    [t insertObject:[teams objectAtIndex:j] atIndex:1];
    

    You’re just inserting more and more objects at the beginning of your t object. You’re never emptying it out. I’m pretty sure that’s not what you want, and it’s probably a performance problem too.

    Second, you’re sending a lot of messages unnecessarily. For example, you’re extracting masks O(N2) times. You can optimize this by extracting all of the masks once.

    NSArray *teams = [options objectForKey:@"teams"];
    NSUInteger teamCount = teams.count;
    
    int masks[teamCount];
    for (NSUInteger i = 0; i < teamCount; ++i) {
        masks[i] = [[teams[i] objectForKey:@"mask"] intValue];
    }
    
    NSMutableArray *matches = [[NSMutableArray alloc] init];
    
    for (NSUInteger i = 0; i < teamCount; ++i) {
        for (NSUInteger j = i + 1; j < teamCount; ++j) {
            if ((masks[i] & masks[j]) == 0) {
                [matches addObject:@[teams[i], teams[j]]];
            }
        }
    }
    

    You’re still doing O(N2) iterations, but you’re doing a lot less work on each iteration.

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

Sidebar

Related Questions

I have a mobile app that I developed using PhoneGap (and html/css/javascript) and there
I am new to mobile application development with PhoneGap. I have created a form
I have an application made by using phonegap 1.5.0 and jquery mobile 1.1.0.... I
i have some code using jquery mobile + phonegap and run it on my
I have a phonegap application targeting iPad that needs to embed a video on
I am developing a mobile application with Phonegap and I need internationalization - display
I am developing an Application with jQuery Mobile and Phonegap. I want to have
I'm developing a web application that uses PhoneGap:Build for a mobile version and want
I have a new mobile application I need to get out the door but
I have to develop a mobile application that is available on Android and iOS.

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.