From aec734100a8cab1fd93109c1a6479a4b91ef8d02 Mon Sep 17 00:00:00 2001 From: Quincy Morgan Date: Thu, 25 Jun 2020 10:08:07 -0400 Subject: [PATCH] Improve general performance when lots of OSM data is downloaded (re: #7656) --- modules/core/difference.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/core/difference.js b/modules/core/difference.js index d24b1cf56..21c8778f2 100644 --- a/modules/core/difference.js +++ b/modules/core/difference.js @@ -1,7 +1,7 @@ import deepEqual from 'fast-deep-equal'; import { geoVecEqual } from '../geo'; -import { utilArrayDifference, utilArrayUnion } from '../util'; +import { utilArrayDifference, utilArrayUnion, utilArrayUniq } from '../util/array'; /* @@ -57,8 +57,16 @@ export function coreDifference(base, head) { } } - for (var headId in head.entities) checkEntityID(headId); - for (var baseId in base.entities) checkEntityID(baseId); + function load() { + // HOT CODE: there can be many thousands of downloaded entities, so looping + // through them all can become a performance bottleneck. Optimize by + // resolving duplicates and using a basic `for` loop + var ids = utilArrayUniq(Object.keys(head.entities).concat(Object.keys(base.entities))); + for (var i = 0; i < ids.length; i++) { + checkEntityID(ids[i]); + } + } + load(); _diff.length = function length() {