<p>I have a Yahoo map with lots of markers (~500). The map performs well enough until I close the page, at which point it pauses (in Firefox) and brings up a "Stop running this script?" dialog (in IE7). If given long enough the script does complete its work.</p>

<p>Is there anything I can do to reduce this delay?</p>

<p>This stripped down code exhibits the problem:</p>

<pre><code>&lt;script type="text/javascript"&gt;
var map = new YMap(document.getElementById('map'));
map.drawZoomAndCenter("Algeria", 17);

for (var i = 0; i &lt; 500; i += 1) {
    var geoPoint = new YGeoPoint((Math.random()-0.5)*180.0, (Math.random()-0.5)*360.0);
    var marker = new YMarker(geoPoint);
    map.addOverlay(marker);
}
&lt;/script&gt; 
</code></pre>

<p>I'm aware of some memory leaks with the event handlers if you're dynamically adding and removing markers, but these are static (though the problem may be related). Oh, and I <em>know</em> this many markers on a map may not be the best way to convey the data, but that's not the answer I'm looking for ;)</p>

<p><strong>Edit:</strong> Following a suggestion below I've tried:</p>

<pre><code>window.onbeforeunload = function() {
    map.removeMarkersAll();
}
</code></pre>

<p>and </p>

<pre><code>window.onbeforeunload = function() {
    mapElement = document.getElementById('map');
    mapElement.parentNode.removeChild(mapElement);
}
</code></pre>

<p>but neither worked :(</p>

[{'answer_id': 83328, 'author': 'Diodeus - James MacFarlane', 'author_id': 12579, 'author_profile': 'https://Stackoverflow.com/users/12579', 'pm_score': 0, 'selected': False, 'text': '<p>You could try removing all the markers, or even removing the map from the DOM using the "onbeforeunload" event.</p>\n'}, {'answer_id': 221162, 'author': 'Joshi Spawnbrood', 'author_id': 15392, 'author_profile': 'https://Stackoverflow.com/users/15392', 'pm_score': 0, 'selected': False, 'text': "<p>Are you sure nothing is tryin to access the map , when you close the window?</p>\n\n<p>I'd do this type of test:</p>\n\n<p>have a wrapper to reach the map itself, and, on unload, have the wrapper block access to map itself.</p>\n"}, {'answer_id': 364640, 'author': 'Kornel', 'author_id': 27009, 'author_profile': 'https://Stackoverflow.com/users/27009', 'pm_score': 1, 'selected': False, 'text': "<p>Use Javascript profiler and see which function is slow. Then you'll have better idea how to make a workaround or at least how to remove expensive cleanup (and let it leak in IE6).</p>\n"}]
['https://Stackoverflow.com/questions/81768', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4914/']