Skip to content

Commit 5a6ea2e

Browse files
committed
Optimize IcaoDecoder.isMilitary() by eliminating regex recompilation
Fixed a critical performance issue where 30+ regex patterns were being compiled on every call to isMilitary(). The patterns are now compiled once as static readonly class fields and reused via .test() method. Performance impact: - Before: 30+ regex compilations per isMilitary() call - After: Zero regex compilations (patterns compiled once at class load) This significantly improves performance for high-throughput ACARS message processing scenarios. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b4b2d1f commit 5a6ea2e

File tree

1 file changed

+136
-37
lines changed

1 file changed

+136
-37
lines changed

lib/IcaoDecoder.ts

Lines changed: 136 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,202 @@ export class IcaoDecoder {
22
name : string;
33
icao : string;
44

5+
// Pre-compiled regex patterns for military ICAO address detection (performance optimization)
6+
private static readonly MILITARY_PATTERNS = {
7+
// US military
8+
US_MIL_1: /^adf[7-9]/,
9+
US_MIL_2: /^adf[a-f]/,
10+
US_MIL_3: /^a[ef]/,
11+
12+
// Egypt military
13+
EGYPT_MIL: /^0100[78]/,
14+
15+
// Algeria military
16+
ALGERIA_MIL: /^0a4/,
17+
18+
// Italy military
19+
ITALY_MIL: /^33ff/,
20+
21+
// France military
22+
FRANCE_MIL_1: /^3a[89a-f]/,
23+
FRANCE_MIL_2: /^3b/,
24+
25+
// Germany military
26+
GERMANY_MIL_1: /^3e[ab]/,
27+
GERMANY_MIL_2: /^3f[4-9ab]/,
28+
29+
// UK military
30+
UK_MIL_1: /^4000[0-3]/,
31+
UK_MIL_2: /^43c/,
32+
33+
// Austria military
34+
AUSTRIA_MIL: /^44[4-7]/,
35+
36+
// Belgium military
37+
BELGIUM_MIL: /^44f/,
38+
39+
// Bulgaria military
40+
BULGARIA_MIL: /^457/,
41+
42+
// Denmark military
43+
DENMARK_MIL: /^45f4/,
44+
45+
// Greece military
46+
GREECE_MIL: /^468[0-3]/,
47+
48+
// Hungary military
49+
HUNGARY_MIL: /^473c0/,
50+
51+
// Norway military
52+
NORWAY_MIL: /^4781/,
53+
54+
// Netherlands military
55+
NETHERLANDS_MIL: /^480/,
56+
57+
// Poland military
58+
POLAND_MIL: /^48d8[0-7]/,
59+
60+
// Portugal military
61+
PORTUGAL_MIL: /^497c/,
62+
63+
// Czech Republic military
64+
CZECH_MIL: /^49842/,
65+
66+
// Switzerland military
67+
SWITZERLAND_MIL: /^4b7/,
68+
69+
// Turkey military
70+
TURKEY_MIL: /^4b82/,
71+
72+
// Slovenia military
73+
SLOVENIA_MIL: /^506f/,
74+
75+
// Oman military
76+
OMAN_MIL: /^70c07/,
77+
78+
// Saudi Arabia military
79+
SAUDI_MIL_1: /^7102[5-8]/,
80+
SAUDI_MIL_2: /^7103[89]/,
81+
82+
// Israel military
83+
ISRAEL_MIL: /^738a/,
84+
85+
// Australia military
86+
AUSTRALIA_MIL_1: /^7c8[2-48]/,
87+
AUSTRALIA_MIL_2: /^7[def]/,
88+
89+
// India military
90+
INDIA_MIL: /^8002/,
91+
92+
// Canada military
93+
CANADA_MIL: /^c[23]/,
94+
95+
// Brazil military
96+
BRAZIL_MIL: /^e4[01]/,
97+
98+
// Chile military
99+
CHILE_MIL: /^e806/,
100+
};
101+
5102
constructor(icao: string) {
6103
this.name = 'icao-decoder-typescript';
7104
this.icao = icao;
8105
}
9106

10107
isMilitary() {
11-
let i = this.icao;
108+
const i = this.icao;
109+
const p = IcaoDecoder.MILITARY_PATTERNS;
110+
12111
return (
13112
false
14113
// us military
15114
//adf7c8-adf7cf = united states mil_5(uf)
16115
//adf7d0-adf7df = united states mil_4(uf)
17116
//adf7e0-adf7ff = united states mil_3(uf)
18117
//adf800-adffff = united states mil_2(uf)
19-
|| i.match(/^adf[7-9]/)
20-
|| i.match(/^adf[a-f]/)
118+
|| p.US_MIL_1.test(i)
119+
|| p.US_MIL_2.test(i)
21120
//ae0000-afffff = united states mil_1(uf)
22-
|| i.match(/^a(e|f)/)
121+
|| p.US_MIL_3.test(i)
23122

24123
//010070-01008f = egypt_mil
25-
|| i.match(/^0100(7|8)/)
124+
|| p.EGYPT_MIL.test(i)
26125

27126
//0a4000-0a4fff = algeria mil(ap)
28-
|| i.match(/^0a4/)
127+
|| p.ALGERIA_MIL.test(i)
29128

30129
//33ff00-33ffff = italy mil(iy)
31-
|| i.match(/^33ff/)
130+
|| p.ITALY_MIL.test(i)
32131

33132
//350000-37ffff = spain mil(sp)
34133
|| (i >= '350000' && i <= '37ffff')
35134

36135
//3a8000-3affff = france mil_1(fs)
37-
|| i.match(/^3a(8|9|[a-f])/)
136+
|| p.FRANCE_MIL_1.test(i)
38137
//3b0000-3bffff = france mil_2(fs)
39-
|| i.match(/^3b/)
138+
|| p.FRANCE_MIL_2.test(i)
40139

41140
//3e8000-3ebfff = germany mil_1(df)
42141
// remove 8 and 9 from mil arnge
43-
|| i.match(/^3e(a|b)/)
142+
|| p.GERMANY_MIL_1.test(i)
44143
//3f4000-3f7fff = germany mil_2(df)
45144
//3f8000-3fbfff = germany mil_3(df)
46-
|| i.match(/^3f([4-9]|[a-b])/)
145+
|| p.GERMANY_MIL_2.test(i)
47146

48147
//400000-40003f = united kingdom mil_1(ra)
49-
|| i.match(/^4000[0-3]/)
148+
|| p.UK_MIL_1.test(i)
50149
//43c000-43cfff = united kingdom mil(ra)
51-
|| i.match(/^43c/)
150+
|| p.UK_MIL_2.test(i)
52151

53152
//444000-447fff = austria mil(aq)
54-
|| (i.match(/^44[4-7]/) && i != '447ac7')
153+
|| (p.AUSTRIA_MIL.test(i) && i != '447ac7')
55154

56155
//44f000-44ffff = belgium mil(bc)
57-
|| i.match(/^44f/)
156+
|| p.BELGIUM_MIL.test(i)
58157

59158
//457000-457fff = bulgaria mil(bu)
60-
|| i.match(/^457/)
159+
|| p.BULGARIA_MIL.test(i)
61160

62161
//45f400-45f4ff = denmark mil(dg)
63-
|| i.match(/^45f4/)
162+
|| p.DENMARK_MIL.test(i)
64163

65164
//468000-4683ff = greece mil(gc)
66-
|| i.match(/^468[0-3]/)
165+
|| p.GREECE_MIL.test(i)
67166

68167
//473c00-473c0f = hungary mil(hm)
69-
|| i.match(/^473c0/)
168+
|| p.HUNGARY_MIL.test(i)
70169

71170
//478100-4781ff = norway mil(nn)
72-
|| i.match(/^4781/)
171+
|| p.NORWAY_MIL.test(i)
73172
//480000-480fff = netherlands mil(nm)
74-
|| i.match(/^480/)
173+
|| p.NETHERLANDS_MIL.test(i)
75174
//48d800-48d87f = poland mil(po)
76-
|| i.match(/^48d8[0-7]/)
175+
|| p.POLAND_MIL.test(i)
77176
//497c00-497cff = portugal mil(pu)
78-
|| i.match(/^497c/)
177+
|| p.PORTUGAL_MIL.test(i)
79178
//498420-49842f = czech republic mil(ct)
80-
|| i.match(/^49842/)
179+
|| p.CZECH_MIL.test(i)
81180

82181
//4b7000-4b7fff = switzerland mil(su)
83-
|| i.match(/^4b7/)
182+
|| p.SWITZERLAND_MIL.test(i)
84183
//4b8200-4b82ff = turkey mil(tq)
85-
|| i.match(/^4b82/)
184+
|| p.TURKEY_MIL.test(i)
86185

87186
//506f00-506fff = slovenia mil(sj)
88-
|| i.match(/^506f/)
187+
|| p.SLOVENIA_MIL.test(i)
89188

90189
//70c070-70c07f = oman mil(on)
91-
|| i.match(/^70c07/)
190+
|| p.OMAN_MIL.test(i)
92191

93192
//710258-71025f = saudi arabia mil_1(sx)
94193
//710260-71027f = saudi arabia mil_2(sx)
95194
//710280-71028f = saudi arabia mil_3(sx)
96195
//710380-71039f = saudi arabia mil_4(sx)
97-
|| i.match(/^7102[5-8]/)
98-
|| i.match(/^7103[8-9]/)
196+
|| p.SAUDI_MIL_1.test(i)
197+
|| p.SAUDI_MIL_2.test(i)
99198

100199
//738a00-738aff = israel mil(iz)
101-
|| i.match(/^738a/)
200+
|| p.ISRAEL_MIL.test(i)
102201

103202
//7c822e-7c822f = australia mil_1(av)
104203
//7c8230-7c823f = australia mil_2(av)
@@ -107,26 +206,26 @@ export class IcaoDecoder {
107206
//7c8300-7c83ff = australia mil_5(av)
108207
//7c8400-7c87ff = australia mil_6(av)
109208
//7c8800-7c8fff = australia mil_7(av)
110-
|| i.match(/^7c8([2-4]|8)/)
209+
|| p.AUSTRALIA_MIL_1.test(i)
111210
//7c9000-7c9fff = australia mil_8(av)
112211
//7ca000-7cbfff = australia mil_9(av)
113212
|| (i >= '7c9000' && i <= '7cbfff')
114213
//7cc000-7cffff = australia mil_10(av) 7cc409 not mil, remove this range
115214
//7d0000-7dffff = australia mil_11(av)
116215
//7e0000-7fffff = australia mil_12(av)
117-
|| i.match(/^7[d-f]/)
216+
|| p.AUSTRALIA_MIL_2.test(i)
118217

119218
//800200-8002ff = india mil(im)
120-
|| i.match(/^8002/)
219+
|| p.INDIA_MIL.test(i)
121220

122221
//c20000-c3ffff = canada mil(cb)
123-
|| i.match(/^c[2-3]/)
222+
|| p.CANADA_MIL.test(i)
124223

125224
//e40000-e41fff = brazil mil(bq)
126-
|| i.match(/^e4[0-1]/)
225+
|| p.BRAZIL_MIL.test(i)
127226

128227
//e80600-e806ff = chile mil(cq)
129-
|| i.match(/^e806/)
228+
|| p.CHILE_MIL.test(i)
130229
);
131230
}
132231

0 commit comments

Comments
 (0)