From e01c36ae3cfd58c46ce961a0ec64a5ed8f802503 Mon Sep 17 00:00:00 2001 From: aminya Date: Wed, 11 Nov 2020 15:40:04 -0600 Subject: [PATCH] fix: use fast union function --- lib/paths-cache.js | 6 +++--- lib/utils.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/paths-cache.js b/lib/paths-cache.js index c2b34e8..136163d 100644 --- a/lib/paths-cache.js +++ b/lib/paths-cache.js @@ -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 () { @@ -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 => diff --git a/lib/utils.js b/lib/utils.js index 1b72125..80ffb4d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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, arr2: Array) { + if (!arr2.length) return + Array.prototype.push.apply(arr1, arr2) +} + +// get unique entries of an array +export function unique(arr: Array) { + return [...new Set(arr)] +} + +// fast union function (replacement for _.union) +export function union(arr1: Array, arr2: Array) { + merge(arr1, arr2) + return unique(arr1) +}