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

  • SEARCH
  • Home
  • 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 7733441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:59:24+00:00 2026-06-01T06:59:24+00:00

I have a snippet of code that creates an array clusterUniqueMarkers that stores all

  • 0

I have a snippet of code that creates an array clusterUniqueMarkers that stores all the unique objects in the array clusterMarkers.

Problem: With the help of console.log(clusterUniqueMarkers.length); I can see that the size of that array blows up! When the code is executed, the browser CPU & memory usage shoots up and crashes. What could have caused the problem?

JS Code

// MarkerClusterer click handler
google.maps.event.addListenerOnce(mc, "clusterclick", function (cluster) {

    // Find number of unique LatLng in clusters
    var clusterMarkers = mc.getMarkers();
    var clusterUniqueMarkers = [];
    for(var j = 0; j < clusterMarkers.length; j++) {

        if(clusterUniqueMarkers.length == 0){
            // Adding first unique marker
            clusterUniqueMarker = new Object();
            clusterUniqueMarker.lat = mc.getMarkers()[j].position.lat();
            clusterUniqueMarker.lng = mc.getMarkers()[j].position.lng();
            clusterUniqueMarkers.push(clusterUniqueMarker);
        } else {
            // At least 1 unique marker
            var clusterUniqueMarkersLength = clusterUniqueMarkers.length;   // prevent infinite loop when clusterUniqueMarkers.length keeps increasing
            console.log(clusterUniqueMarkers.length);
            console.log(clusterUniqueMarkersLength);
            console.log('j: ' + j);
            for(var k = 0; k < clusterUniqueMarkersLength; k++) {
                // If marker is unique
                if(clusterUniqueMarkers[k].lat != mc.getMarkers()[j].position.lat() && clusterUniqueMarkers[k].lng != mc.getMarkers()[j].position.lat()) {
                    clusterUniqueMarker = new Object();
                    clusterUniqueMarker.lat = mc.getMarkers()[j].position.lat();
                    clusterUniqueMarker.lng = mc.getMarkers()[j].position.lng();
                    clusterUniqueMarkers.push(clusterUniqueMarker);
                }
            }
        }

    }
    console.log(clusterMarkers);
    console.log(clusterUniqueMarkers);
}); 
  • 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-01T06:59:25+00:00Added an answer on June 1, 2026 at 6:59 am

    Your logic is wrong. You’re iterating through all the elements in clusterMarkers, and doing this:

    If the unique list is empty, add the current element as a unique one.

    If the unique list is not empty, iterate through the unique list, and if the current element doesn’t match the current unique element, add it as a unique one. What you want to do is say “if the current element doesn’t equal any of the unique ones, add it as a unique one”, which this code will achieve:

    for(var k = 0; k < clusterUniqueMarkersLength; k++) {
        // If marker is unique
        var unique = true;
        if(clusterUniqueMarkers[k].lat == clusterMarkers[j].position.lat() && clusterUniqueMarkers[k].lng == clusterMarkers[j].position.lat()) {
            // if the element in clusterMarkers is equal to an element already in clusterUniqueMarkers
            // it can't be unique
            unique = false;
        }
    }
    if(unique) {
        clusterUniqueMarker = new Object();
        clusterUniqueMarker.lat = mc.getMarkers()[j].position.lat();
        clusterUniqueMarker.lng = mc.getMarkers()[j].position.lng();
        clusterUniqueMarkers.push(clusterUniqueMarker);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a snippet of code that I'm working with to filter rows in
In an Activity, I have some snippet of code that fires off a Message:
I have the following snippet of code that's generating the Use new keyword if
I have a cool snippet of code that works well, except one thing. The
I have following code snippet that i use to compile class at the run
We have a project that generates a code snippet that can be used on
I have code that adds a pre-determined HTML snippet into a pre-determined <div> element
I have a code snippet written in PHP that pulls a block of text
I have this code snippet inside a function that checks if an object exists
I have the PL/SQL code that is similar to the following snippet: create or

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.