Skip to content

Commit 6f50da5

Browse files
committed
refactor: use map instead of object for internal cache
1 parent 1c6c911 commit 6f50da5

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/license-plugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class LicensePlugin {
112112
// This is a cache storing a directory path to associated package.
113113
// This is an improvement to avoid looking for package information for
114114
// already scanned directory.
115-
this._cache = {};
115+
this._cache = new Map();
116116
}
117117

118118
/**
@@ -160,8 +160,8 @@ class LicensePlugin {
160160

161161
while (dir && dir !== this._cwd && !scannedDirs.includes(dir)) {
162162
// Try the cache.
163-
if (_.has(this._cache, dir)) {
164-
pkg = this._cache[dir];
163+
if (this._cache.has(dir)) {
164+
pkg = this._cache.get(dir);
165165
if (pkg) {
166166
this.debug(`found package.json in cache (package: ${pkg.name})`);
167167
this.addDependency(pkg);
@@ -222,7 +222,7 @@ class LicensePlugin {
222222

223223
// Update the cache
224224
scannedDirs.forEach((scannedDir) => {
225-
this._cache[scannedDir] = pkg;
225+
this._cache.set(scannedDir, pkg);
226226
});
227227
}
228228

test/license-plugin.spec.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,9 @@ describe('LicensePlugin', () => {
330330
'fake-package': fakePackage,
331331
});
332332

333-
expect(plugin._cache).toEqual({
334-
[path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg,
335-
[path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg,
336-
});
333+
expect(plugin._cache.size).toBe(2);
334+
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1', 'src'))).toEqual(pkg);
335+
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1'))).toEqual(pkg);
337336
});
338337

339338
it('should load pkg and put null without package', () => {
@@ -342,9 +341,8 @@ describe('LicensePlugin', () => {
342341
plugin.scanDependency(id);
343342

344343
expect(plugin._dependencies).toEqual({});
345-
expect(plugin._cache).toEqual({
346-
[path.normalize(path.join(__dirname, '..', 'src'))]: null,
347-
});
344+
expect(plugin._cache.size).toBe(1);
345+
expect(plugin._cache.get(path.normalize(path.join(__dirname, '..', 'src')))).toBeNull();
348346
});
349347

350348
it('should try to load pkg without leading NULL character ', () => {
@@ -359,19 +357,18 @@ describe('LicensePlugin', () => {
359357
'fake-package': fakePackage,
360358
});
361359

362-
expect(plugin._cache).toEqual({
363-
[path.join(__dirname, 'fixtures', 'fake-package-1', 'src')]: pkg,
364-
[path.join(__dirname, 'fixtures', 'fake-package-1')]: pkg,
365-
});
360+
expect(plugin._cache.size).toBe(2);
361+
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1', 'src'))).toEqual(pkg);
362+
expect(plugin._cache.get(path.join(__dirname, 'fixtures', 'fake-package-1'))).toEqual(pkg);
366363
});
367364

368365
it('should load pkg and use the cache if available', () => {
369366
const existsSync = spyOn(fs, 'existsSync').and.callThrough();
370367
const pkgPath = path.join(__dirname, 'fixtures', 'fake-package-1');
371368
const id = path.join(pkgPath, 'src', 'index.js');
372369

373-
plugin._cache[path.join(pkgPath, 'src')] = null;
374-
plugin._cache[pkgPath] = null;
370+
plugin._cache.set(path.join(pkgPath, 'src'), null);
371+
plugin._cache.set(pkgPath, null);
375372

376373
plugin.scanDependency(id);
377374

0 commit comments

Comments
 (0)