Skip to content

Commit ec4c788

Browse files
committed
chore: own sample function in sample benchmark
1 parent d63477a commit ec4c788

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

benchmark/filter-vs-intersection.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,43 @@ import { RB } from '../dist/index.js'
33

44
const emailRegex = /^[\w\-\.]+@([\w-]+\.)+[\w-]{2,}$/
55

6-
function runIntersection(sampleCount) {
6+
function runIntersection(sampleCount: number) {
77
const startTime = performance.now()
88
const intersection = RB(emailRegex).and(/^.{3,10}$/).toRegExp()
99
fc.sample(fc.stringMatching(intersection), sampleCount)
1010
return performance.now() - startTime
1111
}
1212

13-
function runFilter(count) {
13+
function runFilter(count: number) {
1414
const startTime = performance.now()
1515
fc.sample(fc.stringMatching(emailRegex).filter(
1616
str => 3 <= str.length && str.length <= 10
1717
), count)
1818
return performance.now() - startTime
1919
}
2020

21+
function runOwnSample(sampleCount: number) {
22+
const startTime = performance.now()
23+
RB(emailRegex).and(/^.{3,10}$/).sample().take(sampleCount).toArray()
24+
return performance.now() - startTime
25+
}
26+
2127
for (const sampleCount of [10,50,100,500,1000,2000]) {
2228
const filterTime = runFilter(sampleCount)
2329
const interTime = runIntersection(sampleCount)
30+
const ownSampleTime = runOwnSample(sampleCount)
2431

2532
console.debug('\nsample count:', sampleCount)
2633
console.debug('time (post-hoc filter) : ', filterTime)
2734
console.debug('time (regex intersection) : ', interTime)
35+
console.debug('time (own sample) : ', ownSampleTime)
2836
}
2937

30-
for (const sampleCount of [10_000,20_000,50_000,100_000,1_000_000]) {
38+
for (const sampleCount of [10_000,20_000,50_000,100_000,1_000_000, 2_000_000, 5_000_000]) {
3139
const interTime = runIntersection(sampleCount)
40+
const ownSampleTime = runOwnSample(sampleCount)
3241

3342
console.debug('\nsample count:', sampleCount)
3443
console.debug('time (regex intersection) : ', interTime)
44+
console.debug('time (own sample) : ', ownSampleTime)
3545
}

0 commit comments

Comments
 (0)