Skip to content

Commit

Permalink
fix: use fast union function
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Nov 11, 2020
1 parent 466a378 commit e01c36a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/paths-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import gitIgnoreParser from 'git-ignore-parser';
import _ from 'underscore-plus'
import minimatch from 'minimatch'
import { Directory, File } from 'atom'
import { execPromise } from './utils'
import { execPromise, union } from './utils'

export default class PathsCache extends EventEmitter {
constructor () {
Expand Down Expand Up @@ -184,13 +184,13 @@ export default class PathsCache extends EventEmitter {
}

this._filePathsByProjectDirectory.set(projectDirectory.path,
_.union(filePathsArray, filePaths)
union(filePathsArray, filePaths)
)

// Merge file paths into existing array (which contains file paths for a specific directory)
filePathsArray = this._filePathsByDirectory.get(directory.path) || []
this._filePathsByDirectory.set(directory.path,
_.union(filePathsArray, filePaths)
union(filePathsArray, filePaths)
)

return Promise.all(directories.map(directory =>
Expand Down
18 changes: 18 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,21 @@ export function execPromise(cmd, options) {
});
});
}

// fast merge function
// https://uilicious.com/blog/javascript-array-push-is-945x-faster-than-array-concat/
export function merge(arr1: Array<any>, arr2: Array<any>) {
if (!arr2.length) return
Array.prototype.push.apply(arr1, arr2)
}

// get unique entries of an array
export function unique(arr: Array<any>) {
return [...new Set(arr)]
}

// fast union function (replacement for _.union)
export function union(arr1: Array<any>, arr2: Array<any>) {
merge(arr1, arr2)
return unique(arr1)
}

0 comments on commit e01c36a

Please sign in to comment.