UNPKG

eslint-plugin-green

Version:

ESLint plugin for evaluating and promoting green coding practices

58 lines (57 loc) 2.52 kB
"use strict"; const rule = { meta: { type: 'suggestion', docs: { description: 'Enforce efficient data structure usage', category: 'Performance', recommended: true, }, schema: [], // no options }, create(context) { return { CallExpression(node) { var _a; // Check for inefficient array operations if (node.callee.type === 'MemberExpression') { const callee = node.callee; if (callee.property.type === 'Identifier') { const methodName = callee.property.name; // Check for array includes on large arrays if (methodName === 'includes') { const arrayNode = callee.object; if (arrayNode.type === 'ArrayExpression' && arrayNode.elements.length > 100) { context.report({ node, message: 'Consider using Set for large arrays to improve lookup performance' }); } } // Check for array indexOf on large arrays if (methodName === 'indexOf') { const arrayNode = callee.object; if (arrayNode.type === 'ArrayExpression' && arrayNode.elements.length > 100) { context.report({ node, message: 'Consider using Map for large arrays to improve lookup performance' }); } } } } // Check for Set constructor usage if (node.callee.type === 'Identifier' && node.callee.name === 'Set') { if (((_a = node.arguments[0]) === null || _a === void 0 ? void 0 : _a.type) === 'ArrayExpression' && node.arguments[0].elements.length > 100) { context.report({ node, message: 'Consider using a more memory-efficient data structure for large datasets' }); } } } }; } }; module.exports = rule;