Skip to content
Open
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Changed
- [#403](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/403) Add JS variant of rule GCI3 - Avoid getting size collection in loops

- Correction of various typos in rules documentations

### Changed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move the Changed title above, the "Correction of various typos..." was in Changed section


### Deleted

## [2.2.2] - 2025-03-13
Expand Down
2 changes: 1 addition & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Some are applicable for different technologies.
| CRJVM??? | Persistence Java : Avoid Joints on non indexed columns | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 |
| GCI1 | Calling a Spring repository inside a loop or a stream | The use of Spring repository in a loop or a stream induces unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
| GCI2 | Multiple if-else statements | Using too many conditional if-else statements will impact performance since JVM will have to compare the conditions. Prefer using a switch statement instead of multiple if-else if possible, or refactor your code to reduce conditonnal statements on the same variable. Switch statement has a performance advantage over if – else. | | ✅ | ✅ | 🚀 | ✅ | 🚀 | 🚀 | 🚫 |
| GCI3 | Getting the size of the collection in the loop | When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. | | ✅ | ✅ | 🚀 | 🚫 | 🚀 | 🚫 | 🚫 |
| GCI3 | Getting the size of the collection in the loop | When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. | | ✅ | ✅ | 🚧 | 🚫 | 🚀 | 🚫 | 🚫 |
| GCI4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | 🚫 | 🚫 |
| GCI5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
| GCI7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | 🚫 | 🚀 | ✅ | 🚀 | 🚫 | 🚫 |
Expand Down
14 changes: 14 additions & 0 deletions src/main/rules/GCI3/javascript/GCI3.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
== Why is this an issue?
When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power.
[source,js,data-diff-id="2",data-diff-type="noncompliant"]
----
for (let i = 0; i < array.length; i++) {
console.log(array[array.length]); // Noncompliant
}
----
[source,js,data-diff-id="2",data-diff-type="compliant"]
----
const arrayLength = array.length; // Fetch the length once
for (let i = 0; i < arrayLength; i++) {
console.log(array[arrayLength]); // Compliant
}