Skip to content

Commit

Permalink
Merge pull request #280 from pasieronen/fix/memoize-getnamesbytype
Browse files Browse the repository at this point in the history
Memoize names (increases speed by 10x)
  • Loading branch information
lo1tuma committed May 25, 2021
2 parents 6e0be1b + f9180cc commit 9446d16
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmarks/runtime.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function lintManyFilesWithAllRecommendedRules({ numberOfFiles }) {
describe('runtime', () => {
it('should not take longer as the defined budget to lint many files with the recommended config', () => {
const nodeVersionMultiplier = getNodeVersionMultiplier();
const budget = 70000000 / cpuSpeed * nodeVersionMultiplier;
const budget = 7000000 / cpuSpeed * nodeVersionMultiplier;

const { medianDuration } = runBenchmark(() => {
lintManyFilesWithAllRecommendedRules({ numberOfFiles: 350 });
Expand Down
7 changes: 5 additions & 2 deletions lib/util/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const map = require('ramda/src/map');
const view = require('ramda/src/view');
const assoc = require('ramda/src/assoc');
const allPass = require('ramda/src/allPass');
const memoizeWith = require('ramda/src/memoizeWith');

const INTERFACES = {
BDD: 'BDD',
Expand Down Expand Up @@ -115,12 +116,14 @@ function getNamesByType(type, filterOptions = {}) {
return extractNames(filteredNames);
}

const getNamesByTypeMemoized = memoizeWith((type, options) => JSON.stringify({ type, options }), getNamesByType);

function getTestCaseNames(options) {
return getNamesByType(TYPES.testCase, options);
return getNamesByTypeMemoized(TYPES.testCase, options);
}

function getSuiteNames(options) {
return getNamesByType(TYPES.suite, options);
return getNamesByTypeMemoized(TYPES.suite, options);
}

module.exports = {
Expand Down
16 changes: 0 additions & 16 deletions test/util/namesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ describe('mocha names', () => {
expect(testCaseNames).to.deep.equal([]);
});

it('always returns a new array', () => {
const testCaseNames1 = getTestCaseNames({ modifiersOnly: true });
const testCaseNames2 = getTestCaseNames({ modifiersOnly: true });

expect(testCaseNames1).to.deep.equal(testCaseNames2);
expect(testCaseNames1).to.not.equal(testCaseNames2);
});

it('ignores invalid modifiers', () => {
const testCaseNames = getTestCaseNames({ modifiers: [ 'foo' ], modifiersOnly: true });

Expand Down Expand Up @@ -185,14 +177,6 @@ describe('mocha names', () => {
expect(suiteNames).to.deep.equal([]);
});

it('always returns a new array', () => {
const suiteNames1 = getSuiteNames({ modifiersOnly: true });
const suiteNames2 = getSuiteNames({ modifiersOnly: true });

expect(suiteNames1).to.deep.equal(suiteNames2);
expect(suiteNames1).to.not.equal(suiteNames2);
});

it('ignores invalid modifiers', () => {
const suiteNames = getSuiteNames({ modifiers: [ 'foo' ], modifiersOnly: true });

Expand Down

0 comments on commit 9446d16

Please sign in to comment.