Skip to content

Commit f885062

Browse files
authored
Merge pull request #88 from realityking/internal-set
Use Set as the internal datastructure when using the list form
2 parents 42c7406 + 020be84 commit f885062

File tree

1 file changed

+14
-35
lines changed

1 file changed

+14
-35
lines changed

index.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ module.exports = function(options) {
4141
if (config.isListForm) {
4242
debug('list form of results requested');
4343

44-
tree = removeDups(results);
45-
debug('removed dups from the resulting list');
46-
44+
tree = Array.from(results);
4745
} else {
4846
debug('object form of results requested');
4947

@@ -134,10 +132,10 @@ module.exports._getDependencies = function(config) {
134132

135133
/**
136134
* @param {Config} config
137-
* @return {Object|String[]}
135+
* @return {Object|Set}
138136
*/
139137
function traverse(config) {
140-
let subTree = config.isListForm ? [] : {};
138+
let subTree = config.isListForm ? new Set() : {};
141139

142140
debug('traversing ' + config.filename);
143141

@@ -168,51 +166,32 @@ function traverse(config) {
168166
localConfig.filename = d;
169167

170168
if (localConfig.isListForm) {
171-
subTree = subTree.concat(traverse(localConfig));
169+
for (let item of traverse(localConfig)) {
170+
subTree.add(item);
171+
}
172172
} else {
173173
subTree[d] = traverse(localConfig);
174174
}
175175
}
176176

177177
if (config.isListForm) {
178-
// Prevents redundancy about each memoized step
179-
subTree = removeDups(subTree);
180-
subTree.push(config.filename);
181-
config.visited[config.filename] = config.visited[config.filename].concat(subTree);
182-
178+
subTree.add(config.filename);
179+
config.visited[config.filename].push(...subTree);
183180
} else {
184181
config.visited[config.filename] = subTree;
185182
}
186183

187184
return subTree;
188185
}
189186

190-
/**
191-
* Returns a list of unique items from the array
192-
*
193-
* @param {String[]} list
194-
* @return {String[]}
195-
*/
196-
function removeDups(list) {
197-
const cache = new Set();
198-
const unique = [];
199-
200-
list.forEach(function(item) {
201-
if (!cache.has(item)) {
202-
unique.push(item);
203-
cache.add(item);
204-
}
205-
});
206-
207-
return unique;
208-
}
209-
210187
// Mutate the list input to do a dereferenced modification of the user-supplied list
211188
function dedupeNonExistent(nonExistent) {
212-
const deduped = removeDups(nonExistent);
213-
nonExistent.length = deduped.length;
189+
const deduped = new Set(nonExistent);
190+
nonExistent.length = deduped.size;
214191

215-
for (let i = 0, l = deduped.length; i < l; i++) {
216-
nonExistent[i] = deduped[i];
192+
let i = 0;
193+
for (const elem of deduped) {
194+
nonExistent[i] = elem;
195+
i++;
217196
}
218197
}

0 commit comments

Comments
 (0)