From 850e7d6568129736fc231c61ecc0584f41f2a0e4 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 9 Jan 2013 13:45:04 -0500 Subject: [PATCH] Optimize difference and parentWays --- js/id/graph/graph.js | 15 ++++++++++----- js/id/renderer/map.js | 14 ++++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 519dc9d29..0fb618627 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -46,15 +46,20 @@ iD.Graph.prototype = { }, parentWays: function(id) { - // This is slow and a bad hack. - return _.filter(this.entities, function(e) { - return e && e.type === 'way' && e.nodes.indexOf(id) !== -1; - }); + var o = []; + for (var i in this.entities) { + if (this.entities[i] && + this.entities[i].type === 'way' && + this.entities[i].nodes.indexOf(id) !== -1) { + o.push(this.entities[i]); + } + } + return o; }, parentRelations: function(id) { // This is slow and a bad hack. - return _.filter(this.entities, function(e) { + return _.filter(this.entities, function buildParentRelations(e) { return e && e.type === 'relation' && _.pluck(e.members, 'id').indexOf(id) !== -1; }); diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index f63ee682f..1f15507d0 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -75,17 +75,19 @@ iD.Map = function() { filter = d3.functor(true); } else { var only = {}; - difference.forEach(function buildDifference(id) { + for (var j = 0; j < difference.length; j++) { + var id = difference[j]; only[id] = graph.fetch(id); if (only[id] && only[id].type === 'node') { - graph.parentWays(id).forEach(function buildOnly(parent) { + var parents = graph.parentWays(id); + for (var k = 0; k < parents.length; k++) { // Don't re-fetch parents - if (only[parent.id] === undefined) { - only[parent.id] = graph.fetch(parent.id); + if (only[parents[k].id] === undefined) { + only[parent.id] = graph.fetch(parents[k].id); } - }); + } } - }); + } all = _.compact(_.values(only)); filter = function(d) { return d.accuracy ? d.way in only : d.id in only; }; }