Skip to content

Commit 21bbcc6

Browse files
committed
improve performance in a tree recursive function
1 parent c92079c commit 21bbcc6

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

assets/js/filenavigator.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
FileNavigator.prototype.buildTree = function(path) {
4848
var self = this;
49-
var recursive = function(parent, file, path) {
49+
function recursive(parent, file, path) {
5050
var absName = path ? (path + '/' + file.name) : file.name;
5151
if (parent.name.trim() && path.trim().indexOf(parent.name) !== 0) {
5252
parent.nodes = [];
@@ -63,22 +63,15 @@
6363
}
6464
parent.nodes.push({name: absName, nodes: []});
6565
}
66-
67-
// sort nodes by name
6866
parent.nodes = parent.nodes.sort(function(a, b) {
69-
if (a.name < b.name) {
70-
return -1;
71-
} else if (a.name > b.name) {
72-
return 1;
73-
}
74-
return 0;
67+
return a.name < b.name ? -1 : a.name === b.name ? 0 : 1;
7568
});
7669
};
7770

7871
!self.history.length && self.history.push({name: path, nodes: []});
7972
for (var o in self.fileList) {
80-
var file = self.fileList[o].model;
81-
file.type === 'dir' && recursive(self.history[0], file, path);
73+
var item = self.fileList[o];
74+
item.isFolder() && recursive(self.history[0], item.model, path);
8275
}
8376
};
8477

0 commit comments

Comments
 (0)