Skip to content

Commit

Permalink
Adopt a Map.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 16, 2019
1 parent 6666180 commit 1ce33b5
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/selection/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {EnterNode} from "./enter.js";
import array from "../array.js";
import constant from "../constant.js";

var keyPrefix = "$"; // Protect against keys like “__proto__”.

function bindIndex(parent, group, enter, update, exit, data) {
var i = 0,
node,
Expand Down Expand Up @@ -34,7 +32,7 @@ function bindIndex(parent, group, enter, update, exit, data) {
function bindKey(parent, group, enter, update, exit, data, key) {
var i,
node,
nodeByKeyValue = {},
nodeByKeyValue = new Map,
groupLength = group.length,
dataLength = data.length,
keyValues = new Array(groupLength),
Expand All @@ -44,11 +42,11 @@ function bindKey(parent, group, enter, update, exit, data, key) {
// If multiple nodes have the same key, the duplicates are added to exit.
for (i = 0; i < groupLength; ++i) {
if (node = group[i]) {
keyValues[i] = keyValue = keyPrefix + key.call(node, node.__data__, i, group);
if (keyValue in nodeByKeyValue) {
keyValues[i] = keyValue = key.call(node, node.__data__, i, group) + "";
if (nodeByKeyValue.has(keyValue)) {
exit[i] = node;
} else {
nodeByKeyValue[keyValue] = node;
nodeByKeyValue.set(keyValue, node);
}
}
}
Expand All @@ -57,19 +55,19 @@ function bindKey(parent, group, enter, update, exit, data, key) {
// If there a node associated with this key, join and add it to update.
// If there is not (or the key is a duplicate), add it to enter.
for (i = 0; i < dataLength; ++i) {
keyValue = keyPrefix + key.call(parent, data[i], i, data);
if (node = nodeByKeyValue[keyValue]) {
keyValue = key.call(parent, data[i], i, data) + "";
if (node = nodeByKeyValue.get(keyValue)) {
update[i] = node;
node.__data__ = data[i];
nodeByKeyValue[keyValue] = null;
nodeByKeyValue.delete(keyValue);
} else {
enter[i] = new EnterNode(parent, data[i]);
}
}

// Add any remaining nodes that were not bound to data to exit.
for (i = 0; i < groupLength; ++i) {
if ((node = group[i]) && (nodeByKeyValue[keyValues[i]] === node)) {
if ((node = group[i]) && (nodeByKeyValue.get(keyValues[i]) === node)) {
exit[i] = node;
}
}
Expand Down

0 comments on commit 1ce33b5

Please sign in to comment.