Skip to content

Commit 6fb970c

Browse files
committed
speed optimizations
1 parent 9beeca4 commit 6fb970c

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/App.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,24 @@
100100
};
101101
102102
const entries = await getGeoEntriesInBounds(bounds);
103-
const hashlevel = Math.max(1, Math.min(8, mapZoom / 2));
104-
const uniqueEntries = getUniqueByGeoHash({
103+
let hashlevel = Math.max(1, Math.min(8, mapZoom / 2));
104+
let uniqueEntries = getUniqueByGeoHash({
105105
entries,
106106
hashLength: hashlevel,
107107
scoreField: "page_len",
108108
});
109+
if (uniqueEntries.length > 400) {
110+
console.log("uniqueEntries", uniqueEntries.length, "so reducing");
111+
uniqueEntries.sort((a, b) => b.page_len - a.page_len);
112+
uniqueEntries = uniqueEntries.slice(0, 400);
113+
}
109114
if (
110115
selectedMarker &&
111116
!uniqueEntries.some((entry) => entry.id === selectedMarker.id)
112117
) {
113118
uniqueEntries.push(selectedMarker);
114119
}
120+
console.log("nMarkers", uniqueEntries.length);
115121
116122
addMarkerClasses(uniqueEntries, hashlevel);
117123
markers = uniqueEntries;

src/lib/Map.svelte

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@
6666
label = fullLabel;
6767
}
6868
}
69-
if (marker.displayClass === "selected") {
70-
console.log("Found selected in icon generation", marker);
71-
}
7269
return `
7370
<div class="map-marker marker-display-${marker.displayClass}" >
7471
<div class="marker-icon-circle">
@@ -167,15 +164,13 @@
167164
}
168165
169166
$: if (markers && map) {
170-
console.log("markers changed");
171167
updateMarkers();
172168
}
173169
174170
// Change from window.boundsChangeTimeout to a local variable
175171
let boundsChangeTimeout = null;
176172
177-
function handleBoundsChange(evt) {
178-
console.log("handleBoundsChange", evt);
173+
function handleBoundsChange() {
179174
if (!map || isFlying) return; // Skip if map is flying
180175
181176
const bounds = map.getBounds();
@@ -213,7 +208,6 @@
213208
214209
// Track which markers we've processed to identify removals
215210
const processedIds = new Set();
216-
console.log("marker update");
217211
218212
// Update or add markers
219213
markers.forEach((marker) => {
@@ -265,7 +259,6 @@
265259
mapMarker.on("mouseover", () => {
266260
if (hoveredMarkerId !== marker.id) {
267261
hoveredMarkerId = marker.id;
268-
console.log("mouseover", marker);
269262
updateMarkers();
270263
}
271264
});
@@ -282,9 +275,9 @@
282275
});
283276
284277
// Remove markers that are no longer in the data
285-
for (const [markerId, mapMarker] of existingMapMarkers.entries()) {
278+
for (const [markerId, entry] of existingMapMarkers.entries()) {
286279
if (!processedIds.has(markerId)) {
287-
markerLayer.removeLayer(mapMarker);
280+
markerLayer.removeLayer(entry.existingMarker);
288281
existingMapMarkers.delete(markerId);
289282
}
290283
}

src/lib/geodata.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ const db = new Loki('geodata.db', {
1313

1414
// Create a collection for geo data with optimized configuration
1515
const geoCollection = db.addCollection('geodata', {
16-
indices: ['geo2', 'category'],
16+
indices: ['geo3', 'category'],
1717
adaptiveBinaryIndices: false, // Disable adaptive indices for bulk operations
1818
transactional: false, // Disable transactions for better performance
1919
clone: false, // Disable object cloning for better performance
2020
disableMeta: true // Disable meta properties for better performance
2121
});
2222

2323
const tinyGeoCollection = db.addCollection('tinygeodata', {
24-
indices: ['geo2', 'category'],
24+
indices: ['geo3', 'category'],
2525
adaptiveBinaryIndices: false,
2626
transactional: false,
2727
clone: false,
@@ -30,7 +30,7 @@ const tinyGeoCollection = db.addCollection('tinygeodata', {
3030

3131
/**
3232
* @typedef {Object} GeoDocument
33-
* @property {string} geo2 - Geohash
33+
* @property {string} geo3 - Geohash
3434
* @property {string} category - Category
3535
* @property {number} lat - Latitude
3636
* @property {number} lon - Longitude
@@ -51,7 +51,7 @@ function addLatLonToRows(rows) {
5151
const latLon = ngeohash.decode(row.geohash);
5252
row.lat = latLon.latitude;
5353
row.lon = latLon.longitude;
54-
row.geo2 = row.geohash.substring(0, 2);
54+
row.geo3 = row.geohash.substring(0, 3);
5555
}
5656
}
5757
}
@@ -162,14 +162,17 @@ async function downloadMissingData(urls) {
162162

163163

164164
function queryGeoTable(table, minLat, maxLat, minLon, maxLon) {
165-
const geohashes_2 = ngeohash.bboxes(minLat, minLon, maxLat, maxLon, 2);
166-
return table.find({geo2: { '$in': geohashes_2 }}).filter(doc =>
167-
doc.lat > minLat &&
168-
doc.lat < maxLat &&
169-
doc.lon > minLon &&
170-
doc.lon < maxLon
171-
);
172-
}
165+
const geohashes_3 = ngeohash.bboxes(minLat, minLon, maxLat, maxLon, 3);
166+
console.log("geohashes_3", geohashes_3);
167+
// Use LokiJS chaining to filter by geo3 and lat first
168+
// const data = table.find()
169+
// console.log("data", data.length);
170+
// console.log("geo3 only", data.filter(doc => geohashes_3.includes(doc.geo3)).length);
171+
return table.chain()
172+
.find({ geo3: { '$in': geohashes_3 } })
173+
.where(obj => obj.lat >= minLat && obj.lat <= maxLat && obj.lon >= minLon && obj.lon <= maxLon)
174+
.data();
175+
}
173176

174177
/**
175178
* Get geo entries within bounds - optimized version

0 commit comments

Comments
 (0)