Skip to content

Loading…

Added safe option to avoid cloning nodes and edges. #81

Closed
wants to merge 2 commits into from

3 participants

@timdcrain

Added a 'safe' property to graphProperties, with default false. If true, nodes and edges are not cloned or checked in iterNodes, getNodes, iterEdges and getEdges. Sped up iterations by ~3x.

@computermacgyver

I think this is a nice balance between protecting the internal variables/state and the need for speed/performance (particularly are very large graphs).

@jacomyal jacomyal closed this
@jacomyal
Owner

Some explanations here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 13, 2013
  1. @timdcrain
Commits on Mar 28, 2013
  1. @timdcrain
Showing with 52 additions and 21 deletions.
  1. +52 −21 src/core/graph.js
View
73 src/core/graph.js
@@ -31,7 +31,8 @@ function Graph() {
// - 'outside'
scalingMode: 'inside',
nodesPowRatio: 0.5,
- edgesPowRatio: 0
+ edgesPowRatio: 0,
+ safe: true
};
/**
@@ -632,12 +633,16 @@ function Graph() {
return self.nodesIndex[id];
}) : self.nodes;
- var aCopies = a.map(cloneNode);
- aCopies.forEach(fun);
+ if (self.p.safe) {
+ var aCopies = a.map(cloneNode);
+ aCopies.forEach(fun);
- a.forEach(function(n, i) {
- checkNode(n, aCopies[i]);
- });
+ a.forEach(function(n, i) {
+ checkNode(n, aCopies[i]);
+ });
+ } else {
+ a.forEach(fun);
+ }
return self;
};
@@ -654,12 +659,16 @@ function Graph() {
return self.edgesIndex[id];
}) : self.edges;
- var aCopies = a.map(cloneEdge);
- aCopies.forEach(fun);
+ if (self.p.safe) {
+ var aCopies = a.map(cloneEdge);
+ aCopies.forEach(fun);
- a.forEach(function(e, i) {
- checkEdge(e, aCopies[i]);
- });
+ a.forEach(function(e, i) {
+ checkEdge(e, aCopies[i]);
+ });
+ } else {
+ a.forEach(fun);
+ }
return self;
};
@@ -667,27 +676,49 @@ function Graph() {
/**
* Returns a specific node clone or an array of specified node clones.
* @param {(string|Array.<string>)} ids The ID or an array of node IDs.
- * @return {(Object|Array.<Object>)} The clone or the array of clones.
+ * @return {(Object|Array.<Object>)} If safe, then the clone or the array
+ * of clones. Otherwise the node or
+ * array of nodes.
*/
function getNodes(ids) {
- var a = ((ids instanceof Array ? ids : [ids]) || []).map(function(id) {
- return cloneNode(self.nodesIndex[id]);
- });
+ var a = (ids instanceof Array ? ids : [ids]) || [];
+ var result;
+
+ if (self.p.safe) {
+ result = a.map(function(id) {
+ return cloneNode(self.nodesIndex[id]);
+ });
+ } else {
+ result = a.map(function(id) {
+ return self.nodesIndex[id];
+ });
+ }
- return (ids instanceof Array ? a : a[0]);
+ return (ids instanceof Array ? result : result[0]);
};
/**
* Returns a specific edge clone or an array of specified edge clones.
* @param {(string|Array.<string>)} ids The ID or an array of edge IDs.
- * @return {(Object|Array.<Object>)} The clone or the array of clones.
+ * @return {(Object|Array.<Object>)} If safe, then the clone or the array
+ * of clones. Otherwise the edge or
+ * array of edges.
*/
function getEdges(ids) {
- var a = ((ids instanceof Array ? ids : [ids]) || []).map(function(id) {
- return cloneEdge(self.edgesIndex[id]);
- });
+ var a = (ids instanceof Array ? ids : [ids]) || [];
+ var result;
+
+ if (self.p.safe) {
+ result = a.map(function(id) {
+ return cloneEdge(self.edgesIndex[id]);
+ });
+ } else {
+ result = a.map(function(id) {
+ return self.edgesIndex[id];
+ });
+ }
- return (ids instanceof Array ? a : a[0]);
+ return (ids instanceof Array ? result : result[0]);
};
empty();
Something went wrong with that request. Please try again.