Skip to content

Commit 6f265d5

Browse files
committed
good-practices powershell
1 parent d19fffc commit 6f265d5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

good-practices/foreach-object.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# BAD
2+
@(1, 2, 3) | Foreach-Object {
3+
# do stuff
4+
}
5+
6+
# GOOD - 2x faster
7+
foreach ($i in @(1, 2, 3)) {
8+
# do stuff
9+
}

good-practices/where-object.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BAD
2+
$array = @(1, 2, 3, 1, 3, 4) | Where-Object { $_ -eq 1 }
3+
4+
# GOOD - x17 faster
5+
$array = @(
6+
foreach ($i in @(1, 2, 3, 1, 3, 4)) {
7+
if ($i -eq 1) {
8+
$i
9+
}
10+
}
11+
)

0 commit comments

Comments
 (0)