Skip to content

Commit 9c61604

Browse files
committed
Standardize iteration syntax
Replaced inline loops with the standardized `iterations` option in benchmark tests to improve readability and maintain consistency. Updated multiple benchmark scenarios across modules, ensuring uniformity in test structure and settings.
1 parent f71be0c commit 9c61604

File tree

2 files changed

+116
-80
lines changed

2 files changed

+116
-80
lines changed

src/format/number.bench.ts

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -100,65 +100,81 @@ describe('formatVnPercent - typical usage scenarios', () => {
100100
})
101101

102102
describe('formatVnNumber - realistic batch operations', () => {
103-
bench('format 50 order counts', () => {
104-
for (let i = 0; i < 50; i++) {
103+
bench(
104+
'format 50 order counts',
105+
() => {
105106
const count = Math.floor(Math.random() * 10000)
106107
formatVnNumber(count)
107-
}
108-
})
108+
},
109+
{ iterations: 50 },
110+
)
109111

110-
bench('format 50 product quantities', () => {
111-
for (let i = 0; i < 50; i++) {
112+
bench(
113+
'format 50 product quantities',
114+
() => {
112115
const quantity = Math.floor(Math.random() * 1000)
113116
formatVnNumber(quantity)
114-
}
115-
})
117+
},
118+
{ iterations: 50 },
119+
)
116120
})
117121

118122
describe('formatVnCurrency - realistic batch operations', () => {
119-
bench('format 50 e-commerce prices (10k-50M VND)', () => {
120-
for (let i = 0; i < 50; i++) {
123+
bench(
124+
'format 50 e-commerce prices (10k-50M VND)',
125+
() => {
121126
const price = Math.floor(Math.random() * 49990000) + 10000
122127
formatVnCurrency(price)
123-
}
124-
})
128+
},
129+
{ iterations: 50 },
130+
)
125131

126-
bench('format 50 invoice totals (100k-100M VND)', () => {
127-
for (let i = 0; i < 50; i++) {
132+
bench(
133+
'format 50 invoice totals (100k-100M VND)',
134+
() => {
128135
const total = Math.floor(Math.random() * 99900000) + 100000
129136
formatVnCurrency(total)
130-
}
131-
})
137+
},
138+
{ iterations: 50 },
139+
)
132140

133-
bench('format 50 financial amounts (1M-10B VND)', () => {
134-
for (let i = 0; i < 50; i++) {
141+
bench(
142+
'format 50 financial amounts (1M-10B VND)',
143+
() => {
135144
const amount = Math.floor(Math.random() * 9999000000) + 1000000
136145
formatVnCurrency(amount)
137-
}
138-
})
146+
},
147+
{ iterations: 50 },
148+
)
139149
})
140150

141151
describe('formatVnPercent - realistic batch operations', () => {
142-
bench('format 50 growth rates (-50% to +150%)', () => {
143-
for (let i = 0; i < 50; i++) {
152+
bench(
153+
'format 50 growth rates (-50% to +150%)',
154+
() => {
144155
const growth = Math.random() * 2 - 0.5
145156
formatVnPercent(growth)
146-
}
147-
})
157+
},
158+
{ iterations: 50 },
159+
)
148160

149-
bench('format 50 conversion rates (0-10%)', () => {
150-
for (let i = 0; i < 50; i++) {
161+
bench(
162+
'format 50 conversion rates (0-10%)',
163+
() => {
151164
const rate = Math.random() * 0.1
152165
formatVnPercent(rate)
153-
}
154-
})
166+
},
167+
{ iterations: 50 },
168+
)
155169

156-
bench('format 50 completion rates (0-100%)', () => {
157-
for (let i = 0; i < 50; i++) {
170+
bench(
171+
'format 50 completion rates (0-100%)',
172+
() => {
158173
const completion = Math.random()
159174
formatVnPercent(completion)
160-
}
161-
})
175+
},
176+
{ iterations: 50 },
177+
)
162178
})
163179

164180
describe('real-world application scenarios', () => {
@@ -207,10 +223,11 @@ describe('real-world application scenarios', () => {
207223
formatVnCurrency(total)
208224
})
209225

210-
bench('display invoice (10 line items)', () => {
211-
let subtotal = 0
226+
bench(
227+
'display invoice (10 line items)',
228+
() => {
229+
let subtotal = 0
212230

213-
for (let i = 0; i < 10; i++) {
214231
const quantity = Math.floor(Math.random() * 20) + 1
215232
const unitPrice = Math.floor(Math.random() * 5000000) + 50000
216233
const lineTotal = quantity * unitPrice
@@ -219,17 +236,18 @@ describe('real-world application scenarios', () => {
219236
formatVnNumber(quantity)
220237
formatVnCurrency(unitPrice)
221238
formatVnCurrency(lineTotal)
222-
}
223239

224-
const taxRate = 0.1
225-
const taxAmount = subtotal * taxRate
226-
const total = subtotal + taxAmount
240+
const taxRate = 0.1
241+
const taxAmount = subtotal * taxRate
242+
const total = subtotal + taxAmount
227243

228-
formatVnCurrency(subtotal)
229-
formatVnPercent(taxRate)
230-
formatVnCurrency(taxAmount)
231-
formatVnCurrency(total)
232-
})
244+
formatVnCurrency(subtotal)
245+
formatVnPercent(taxRate)
246+
formatVnCurrency(taxAmount)
247+
formatVnCurrency(total)
248+
},
249+
{ iterations: 10 },
250+
)
233251

234252
bench('display financial dashboard (weekly metrics)', () => {
235253
const weeklyData = Array.from({ length: 7 }, () => ({
@@ -257,8 +275,9 @@ describe('real-world application scenarios', () => {
257275
formatVnNumber(avgOrders)
258276
})
259277

260-
bench('display product catalog (20 products)', () => {
261-
for (let i = 0; i < 20; i++) {
278+
bench(
279+
'display product catalog (20 products)',
280+
() => {
262281
const price = Math.floor(Math.random() * 10000000) + 10000
263282
const discount = Math.random() * 0.5
264283
const finalPrice = price * (1 - discount)
@@ -272,8 +291,9 @@ describe('real-world application scenarios', () => {
272291
formatVnNumber(stock)
273292
formatVnNumber(sold)
274293
formatVnNumber(rating)
275-
}
276-
})
294+
},
295+
{ iterations: 20 },
296+
)
277297

278298
bench('display sales report (monthly performance)', () => {
279299
const monthlyData = Array.from({ length: 12 }, () => ({
@@ -312,8 +332,9 @@ describe('real-world application scenarios', () => {
312332
formatVnCurrency(total)
313333
})
314334

315-
bench('display data table (50 rows × 6 columns)', () => {
316-
for (let i = 0; i < 50; i++) {
335+
bench(
336+
'display data table (50 rows × 6 columns)',
337+
() => {
317338
const orderId = Math.floor(Math.random() * 100000)
318339
const quantity = Math.floor(Math.random() * 100) + 1
319340
const unitPrice = Math.floor(Math.random() * 5000000) + 10000
@@ -327,8 +348,9 @@ describe('real-world application scenarios', () => {
327348
formatVnCurrency(total)
328349
formatVnPercent(discount)
329350
formatVnPercent(status)
330-
}
331-
})
351+
},
352+
{ iterations: 50 },
353+
)
332354
})
333355

334356
describe('input type variations - real-world scenarios', () => {
@@ -380,15 +402,17 @@ describe('combined operations - real-world scenarios', () => {
380402
formatVnNumber(orders)
381403
})
382404

383-
bench('format complete product listing (30 products)', () => {
384-
for (let i = 0; i < 30; i++) {
405+
bench(
406+
'format complete product listing (30 products)',
407+
() => {
385408
const price = Math.floor(Math.random() * 10000000) + 10000
386409
const discount = Math.random() * 0.5
387410
const stock = Math.floor(Math.random() * 1000) + 1
388411

389412
formatVnCurrency(price)
390413
formatVnPercent(discount)
391414
formatVnNumber(stock)
392-
}
393-
})
415+
},
416+
{ iterations: 30 },
417+
)
394418
})

src/read/index.bench.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,41 @@ describe('readVnNumber - typical usage scenarios', () => {
2828
})
2929

3030
describe('readVnNumber - realistic batch operations', () => {
31-
bench('read 50 product quantities (1-100)', () => {
32-
for (let i = 0; i < 50; i++) {
31+
bench(
32+
'read 50 product quantities (1-100)',
33+
() => {
3334
const quantity = Math.floor(Math.random() * 100) + 1
3435
readVnNumber(quantity)
35-
}
36-
})
36+
},
37+
{ iterations: 50 },
38+
)
3739

38-
bench('read 50 e-commerce prices (10k-50M VND)', () => {
39-
for (let i = 0; i < 50; i++) {
40+
bench(
41+
'read 50 e-commerce prices (10k-50M VND)',
42+
() => {
4043
const price = Math.floor(Math.random() * 49990000) + 10000
4144
readVnNumber(price)
42-
}
43-
})
45+
},
46+
{ iterations: 50 },
47+
)
4448

45-
bench('read 50 invoice totals (100k-100M VND)', () => {
46-
for (let i = 0; i < 50; i++) {
49+
bench(
50+
'read 50 invoice totals (100k-100M VND)',
51+
() => {
4752
const total = Math.floor(Math.random() * 99900000) + 100000
4853
readVnNumber(total)
49-
}
50-
})
54+
},
55+
{ iterations: 50 },
56+
)
5157

52-
bench('read 50 financial amounts (1M-10B VND)', () => {
53-
for (let i = 0; i < 50; i++) {
58+
bench(
59+
'read 50 financial amounts (1M-10B VND)',
60+
() => {
5461
const amount = Math.floor(Math.random() * 9999000000) + 1000000
5562
readVnNumber(amount)
56-
}
57-
})
63+
},
64+
{ iterations: 50 },
65+
)
5866
})
5967

6068
describe('readVnNumber - real-world application scenarios', () => {
@@ -74,17 +82,19 @@ describe('readVnNumber - real-world application scenarios', () => {
7482
})
7583
})
7684

77-
bench('display invoice line items (10 items)', () => {
78-
for (let i = 0; i < 10; i++) {
85+
bench(
86+
'display invoice line items (10 items)',
87+
() => {
7988
const quantity = Math.floor(Math.random() * 20) + 1
8089
const unitPrice = Math.floor(Math.random() * 5000000) + 50000
8190
const lineTotal = quantity * unitPrice
8291

8392
readVnNumber(quantity)
8493
readVnNumber(unitPrice)
8594
readVnNumber(lineTotal)
86-
}
87-
})
95+
},
96+
{ iterations: 10 },
97+
)
8898

8999
bench('display financial dashboard (20 metrics)', () => {
90100
const metrics = {
@@ -113,17 +123,19 @@ describe('readVnNumber - real-world application scenarios', () => {
113123
})
114124
})
115125

116-
bench('display product catalog (20 products)', () => {
117-
for (let i = 0; i < 20; i++) {
126+
bench(
127+
'display product catalog (20 products)',
128+
() => {
118129
const price = Math.floor(Math.random() * 10000000) + 10000
119130
const stock = Math.floor(Math.random() * 1000) + 1
120131
const sold = Math.floor(Math.random() * 5000)
121132

122133
readVnNumber(price)
123134
readVnNumber(stock)
124135
readVnNumber(sold)
125-
}
126-
})
136+
},
137+
{ iterations: 20 },
138+
)
127139

128140
bench('display payment receipt (typical transaction)', () => {
129141
const subtotal = 5450000

0 commit comments

Comments
 (0)