Skip to content

Commit

Permalink
trade some space savings for less agressive circular reference algori…
Browse files Browse the repository at this point in the history
…thm (the same as console.dir it seems)
  • Loading branch information
James Halliday committed Feb 18, 2011
1 parent d190897 commit ee52d80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ function walk (root, cb) {

if (typeof node == 'object' && node !== null) {
state.isLeaf = Object.keys(node).length == 0
var circs = parents.filter(function (p) {
return node == p.node
});
if (circs.length) state.circular = circs[0];

for (var i = 0; i < parents.length; i++) {
if (parents[i].node === node) {
state.circular = parents[i];
break;
}
}
}
else {
state.isLeaf = true;
Expand Down
5 changes: 2 additions & 3 deletions test/circular.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ exports.doubleCirc = function () {
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] };
obj.y[2] = obj;
obj.x.push(obj.y);
console.dir(obj);

var circs = [];
Traverse(obj).forEach(function (x) {
Expand All @@ -47,8 +46,8 @@ exports.doubleCirc = function () {
}
});

assert.eql(circs[0].self.path, [ 'x', 3 ]);
assert.eql(circs[0].circ.path, [ 'y' ]);
assert.eql(circs[0].self.path, [ 'x', 3, 2 ]);
assert.eql(circs[0].circ.path, []);

assert.eql(circs[1].self.path, [ 'y', 2 ]);
assert.eql(circs[1].circ.path, []);
Expand Down

0 comments on commit ee52d80

Please sign in to comment.