Avoiding Event Loop Blocking in JavaScript Applications
Best practices for preventing event loop blocking to maintain responsive JavaScript applications.
0 likes
10 views
Rule Content
{ "title": "Avoiding Event Loop Blocking in JavaScript Applications", "description": "Best practices for preventing event loop blocking to maintain responsive JavaScript applications.", "category": "JavaScript Cursor Rules", "rules": [ { "id": "no-synchronous-io", "description": "Avoid using synchronous I/O operations to prevent blocking the event loop.", "severity": "error", "pattern": [ { "type": "CallExpression", "callee": { "type": "MemberExpression", "object": { "type": "Identifier", "name": "fs" }, "property": { "type": "Identifier", "name": "readFileSync" } } }, { "type": "CallExpression", "callee": { "type": "MemberExpression", "object": { "type": "Identifier", "name": "fs" }, "property": { "type": "Identifier", "name": "writeFileSync" } } }, { "type": "CallExpression", "callee": { "type": "MemberExpression", "object": { "type": "Identifier", "name": "fs" }, "property": { "type": "Identifier", "name": "appendFileSync" } } }, { "type": "CallExpression", "callee": { "type": "MemberExpression", "object": { "type": "Identifier", "name": "fs" }, "property": { "type": "Identifier", "name": "unlinkSync" } } } ], "message": "Use asynchronous file system methods instead of synchronous ones to avoid blocking the event loop." }, { "id": "no-blocking-loops", "description": "Avoid long-running synchronous loops that can block the event loop.", "severity": "error", "pattern": [ { "type": "WhileStatement", "test": { "type": "Literal", "value": true } }, { "type": "ForStatement", "test": { "type": "Literal", "value": true } } ], "message": "Refactor long-running loops to use asynchronous techniques like setTimeout or process.nextTick to prevent blocking the event loop." }, { "id": "no-heavy-computation", "description": "Offload heavy computations to prevent blocking the event loop.", "severity": "error", "pattern": [ { "type": "CallExpression", "callee": { "type": "Identifier", "name": "computePrimes" } }, { "type": "CallExpression", "callee": { "type": "Identifier", "name": "calculateFibonacci" } } ], "message": "Consider using Web Workers or child processes to handle CPU-intensive tasks without blocking the event loop." }, { "id": "no-unoptimized-regex", "description": "Avoid using inefficient regular expressions that can cause event loop blocking.", "severity": "error", "pattern": [ { "type": "Literal", "value": "/(\\/.*)+$/" } ], "message": "Optimize regular expressions to prevent performance issues and potential event loop blocking." }, { "id": "no-large-json-operations", "description": "Avoid processing large JSON objects synchronously to prevent blocking the event loop.", "severity": "error", "pattern": [ { "type": "CallExpression", "callee": { "type": "Identifier", "name": "JSON.parse" }, "arguments": [ { "type": "Identifier", "name": "largeJsonString" } ] }, { "type": "CallExpression", "callee": { "type": "Identifier", "name": "JSON.stringify" }, "arguments": [ { "type": "Identifier", "name": "largeJsonObject" } ] } ], "message": "Use streaming parsers or chunk processing for large JSON data to avoid blocking the event loop." } ] }