Skip to content

Commit

Permalink
Minor improvements to trait counting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jameshadfield committed Jan 30, 2024
1 parent c4145be commit ecc83eb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/util/treeCountingHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ecc83eb

Please sign in to comment.