Skip to content

Commit 15cad85

Browse files
Safaa OugunirSafaa Ougunir
authored andcommitted
feat: add Js rule GCI3
1 parent 458ab17 commit 15cad85

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

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

1414
- Correction of various typos in rules documentations
1515

RULES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Some are applicable for different technologies.
2626
| CRJVM??? | Persistence Java : Avoid Joints on non indexed columns | | | 🚧 |||||| 🚫 |
2727
| 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. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
2828
| 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. | ||| 🚀 || 🚀 | 🚀 | 🚫 |
29-
| 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. | ||| 🚀 | 🚫 | 🚀 | 🚫 | 🚫 |
29+
| 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. | ||| 🚧 | 🚫 | 🚀 | 🚫 | 🚫 |
3030
| 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/) | 🚫 || 🚀 || 🚀 | 🚫 | 🚫 |
3131
| 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. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
3232
| 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/) | 🚫 | 🚫 | 🚀 || 🚀 | 🚫 | 🚫 |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
== Why is this an issue?
2+
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.
3+
[source,js,data-diff-id="2",data-diff-type="noncompliant"]
4+
----
5+
for (let i = 0; i < array.length; i++) {
6+
console.log(array[array.length]); // Noncompliant
7+
}
8+
----
9+
[source,js,data-diff-id="2",data-diff-type="compliant"]
10+
----
11+
const arrayLength = array.length; // Fetch the length once
12+
for (let i = 0; i < arrayLength; i++) {
13+
console.log(array[arrayLength]); // Compliant
14+
}

0 commit comments

Comments
 (0)