Skip to content

Commit b95d113

Browse files
authored
Merge pull request #34 from dschenkelman/prBranch
Avoid large memory allocations when doing forEach in case netmask is large (e.g. /8)
2 parents 610161f + a4ee31c commit b95d113

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/netmask.coffee

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ class Netmask
8484
return new Netmask(long2ip(@netLong + (@size * count)), @mask)
8585

8686
forEach: (fn) ->
87-
range = [ip2long(@first)..ip2long(@last)]
88-
fn long2ip(long), long, index for long, index in range
87+
# this implementation is not idiomatic but avoids large memory allocations (2 arrays, one for range and one for the results) in cases when then netmask is large
88+
long = ip2long(@first)
89+
lastLong = ip2long(@last)
90+
index = 0
91+
while long <= lastLong
92+
fn long2ip(long), long, index
93+
index++
94+
long++
95+
return
8996

9097
# Returns the complete netmask formatted as `base/bitmask`
9198
toString: ->

0 commit comments

Comments
 (0)