From ecc83eb73e56c58cf1530dd4f446fcda00df0059 Mon Sep 17 00:00:00 2001 From: james hadfield Date: Tue, 30 Jan 2024 10:16:39 +1300 Subject: [PATCH] Minor improvements to trait counting Deferring the value lookup will be slightly faster, especially for the (common) terminal-nodes-only case. We have no use for counting invalid values, so skip these. --- src/util/treeCountingHelpers.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/util/treeCountingHelpers.js b/src/util/treeCountingHelpers.js index ffcc140c0..b90cd34f7 100644 --- a/src/util/treeCountingHelpers.js +++ b/src/util/treeCountingHelpers.js @@ -15,7 +15,6 @@ export const countTraitsAcrossTree = (nodes, traits, visibility, terminalOnly) = nodes.forEach((node) => { traits.forEach((trait) => { // traits are "country" or "author" etc - const value = getTraitFromNode(node, trait); // value is "USA", "black" etc if (terminalOnly && node.hasChildren) { return; @@ -25,8 +24,10 @@ export const countTraitsAcrossTree = (nodes, traits, visibility, terminalOnly) = return; } - const currentValue = counts[trait].get(value) || 0; - counts[trait].set(value, currentValue+1); + const value = getTraitFromNode(node, trait); // value is "USA", "black" etc + if (value===undefined) return; // check for invalid values + const currentValueCount = counts[trait].get(value) || 0; + counts[trait].set(value, currentValueCount+1); }); }); return counts;