Skip to content

Commit

Permalink
improve traverse perf by using for-loops
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed May 24, 2015
1 parent 9a25400 commit 3a77e45
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions index.js
Expand Up @@ -20,21 +20,21 @@ function parse (src, opts) {

var traverse = function (node, cb) {
if (Array.isArray(node)) {
node.forEach(function (x) {
if(x != null) {
x.parent = node;
traverse(x, cb);
for (var i = 0; i < node.length; i++) {
if (node[i] != null) {
node[i].parent = node;
traverse(node[i], cb);
}
});
}
}
else if (node && typeof node === 'object') {
cb(node);

Object.keys(node).forEach(function (key) {
if (key === 'parent' || !node[key]) return;
for (var key in node) {
if (!node.hasOwnProperty(key)) continue;
if (key === 'parent' || !node[key]) continue;
node[key].parent = node;
traverse(node[key], cb);
});
}
}
};

Expand Down

0 comments on commit 3a77e45

Please sign in to comment.