Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup Performance Optimization #214

Merged
merged 3 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"overrides": [
{
"files": [ "test/**/*.js" ],
"files": [ "test/**/*.js", "benchmarks/**/*.js" ],
"env": { "mocha": true },
"rules": {
"max-nested-callbacks": [ "error", 8 ]
Expand Down
20 changes: 20 additions & 0 deletions benchmarks/startup.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const os = require('os');
const { expect } = require('chai');
const { performance } = require('perf_hooks');

const [ { speed: cpuSpeed } ] = os.cpus();

describe('startup / require time', () => {
it('should not take longer as the defined budget to require the plugin', () => {
const budget = 75000 / cpuSpeed;

const startTime = performance.now();
require('../index');
const endTime = performance.now();
const loadTime = endTime - startTime;

expect(loadTime).to.be.below(budget);
});
});
4 changes: 2 additions & 2 deletions lib/rules/handle-done-callback.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const R = require('ramda');
const find = require('ramda/src/find');
const astUtils = require('../util/ast');

module.exports = function (context) {
Expand All @@ -9,7 +9,7 @@ module.exports = function (context) {
}

function findParamInScope(paramName, scope) {
return R.find(function (variable) {
return find(function (variable) {
return variable.name === paramName && variable.defs[0].type === 'Parameter';
}, scope.variables);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-top-level-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Alexander Afanasyev
*/

const R = require('ramda');
const isNil = require('ramda/src/isNil');
const astUtil = require('../util/ast');
const { additionalSuiteNames } = require('../util/settings');

Expand All @@ -18,7 +18,7 @@ module.exports = function (context) {
const settings = context.settings;
let suiteLimit;

if (R.isNil(options.limit)) {
if (isNil(options.limit)) {
suiteLimit = defaultSuiteLimit;
} else {
suiteLimit = options.limit;
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-mocha-arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Paul Melnikow
*/

const R = require('ramda');
const last = require('ramda/src/last');
const astUtils = require('../util/ast');

module.exports = function (context) {
Expand All @@ -25,7 +25,7 @@ module.exports = function (context) {
}

if (fn.params.length > 0) {
paramsFullText = `(${ sourceCode.text.slice(fn.params[0].start, R.last(fn.params).end) })`;
paramsFullText = `(${ sourceCode.text.slice(fn.params[0].start, last(fn.params).end) })`;
}

return `${functionKeyword}${paramsFullText} `;
Expand Down
7 changes: 4 additions & 3 deletions lib/rules/no-synchronous-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const R = require('ramda');
const isNil = require('ramda/src/isNil');
const find = require('ramda/src/find');
const astUtil = require('../util/ast');

const asyncMethods = [ 'async', 'callback', 'promise' ];
Expand All @@ -14,7 +15,7 @@ function isAsyncFunction(functionExpression) {
}

function findPromiseReturnStatement(nodes) {
return R.find(function (node) {
return find(function (node) {
return node.type === 'ReturnStatement' && node.argument && node.argument.type !== 'Literal';
}, nodes);
}
Expand All @@ -36,7 +37,7 @@ function doesReturnPromise(functionExpression) {

module.exports = function (context) {
const options = context.options[0] || {};
const allowedAsyncMethods = R.isNil(options.allowed) ? asyncMethods : options.allowed;
const allowedAsyncMethods = isNil(options.allowed) ? asyncMethods : options.allowed;

function check(node) {
if (astUtil.hasParentMochaFunctionCall(node)) {
Expand Down
17 changes: 11 additions & 6 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

const R = require('ramda');
const complement = require('ramda/src/complement');
const both = require('ramda/src/both');
const isNil = require('ramda/src/isNil');
const propEq = require('ramda/src/propEq');
const pathEq = require('ramda/src/pathEq');
const find = require('ramda/src/find');

const isDefined = R.complement(R.isNil);
const isCallExpression = R.both(isDefined, R.propEq('type', 'CallExpression'));
const isDefined = complement(isNil);
const isCallExpression = both(isDefined, propEq('type', 'CallExpression'));

const describeAliases = [
'describe', 'xdescribe', 'describe.only', 'describe.skip',
Expand Down Expand Up @@ -59,9 +64,9 @@ function isTestCase(node) {
}

function findReference(scope, node) {
const hasSameRangeAsNode = R.pathEq([ 'identifier', 'range' ], node.range);
const hasSameRangeAsNode = pathEq([ 'identifier', 'range' ], node.range);

return R.find(hasSameRangeAsNode, scope.references);
return find(hasSameRangeAsNode, scope.references);
}

function isShadowed(scope, identifier) {
Expand Down Expand Up @@ -103,7 +108,7 @@ function isReturnOfUndefined(node) {
return isImplicitUndefined || isExplicitUndefined(argument);
}

const findReturnStatement = R.find(R.propEq('type', 'ReturnStatement'));
const findReturnStatement = find(propEq('type', 'ReturnStatement'));

module.exports = {
isDescribe,
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
],
"scripts": {
"pretest": "eslint .",
"test": "npm run test:unit:with-coverage",
"test": "npm run test:unit:with-coverage && npm run test:bench",
"test:unit": "mocha test --recursive --reporter dot",
"test:unit:with-coverage": "nyc npm run test:unit",
"test:bench": "mocha benchmarks",
"coveralls": "cat ./build/coverage/lcov.info | coveralls",
"changelog": "pr-log"
},
Expand Down Expand Up @@ -64,7 +65,8 @@
"branches": 100,
"exclude": [
"build",
"test"
"test",
"benchmarks/"
],
"reporter": [
"lcov",
Expand Down