agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
73 lines (63 loc) • 2.13 kB
JavaScript
/**
* @file Algorithmic performance analysis
* @description Analyzes code for algorithmic performance issues
*/
/**
* Detect quadratic algorithmic patterns in code
* @param {string} content - Code content to analyze
* @returns {Array} Array of quadratic pattern issues
*/
function detectQuadraticPatterns(content) {
// Disabled to reduce false positives
return [];
const patterns = [];
const lines = content.split('\n');
// Look for nested loops
let nestedLoopDepth = 0;
let currentLoopStart = -1;
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
const trimmed = line.trim();
// Detect loop starts
if (trimmed.match(/for\s*\(|while\s*\(|forEach\s*\(/)) {
if (nestedLoopDepth === 0) {
currentLoopStart = index;
}
nestedLoopDepth++;
// If we have nested loops, check for O(n²) patterns
if (nestedLoopDepth >= 2) {
patterns.push({
type: 'nested_loops',
startLine: currentLoopStart + 1,
currentLine: index + 1,
content: trimmed,
severity: 'HIGH',
description: 'Nested loops detected - potential O(n²) complexity',
suggestion: 'Consider using hash maps, sorting, or other optimization techniques'
});
}
}
// Detect loop ends (simplified)
if (trimmed.includes('}') && nestedLoopDepth > 0) {
nestedLoopDepth = Math.max(0, nestedLoopDepth - 1);
}
// Look for array operations inside loops
if (nestedLoopDepth > 0) {
if (trimmed.includes('.find(') || trimmed.includes('.filter(') ||
trimmed.includes('.indexOf(') || trimmed.includes('.includes(')) {
patterns.push({
type: 'array_operation_in_loop',
line: index + 1,
content: trimmed,
severity: 'MEDIUM',
description: 'Array operation inside loop - potential performance issue',
suggestion: 'Pre-compute or use Set/Map for faster lookups'
});
}
}
}
return patterns;
}
module.exports = {
detectQuadraticPatterns
};