11import fs from 'fs'
2- import { parseRegexString } from '../dist/regex-parser.js'
3- import { RB } from '../dist/index.js'
2+ import { parseRegExp } from '../dist/regex-parser.js'
43
54export function * readDataset ( ) {
65 const jsonStr = fs . readFileSync ( './benchmark/regex-dataset.json' , 'utf-8' )
@@ -17,29 +16,37 @@ export function* readDataset() {
1716 }
1817}
1918
20- let hasError = 0
21- let noError = 0
22- let totalParseTime = 0
19+ const dataset = [ ...readDataset ( ) ]
2320
24- for ( const { regex, flags } of readDataset ( ) ) {
21+ let parseErrorCount = 0
22+
23+ for ( const { regex, flags } of dataset ) {
2524 try {
2625 // parseRegexString(regex)
2726 const regexp = new RegExp ( regex , flags )
2827 console . log ( '====' , regexp , '====' )
2928
30- const timeStart = performance . now ( )
31- const parsed = RB ( regexp )
32- const timeEnd = performance . now ( )
29+ performance . mark ( 'parse-start' )
30+ const parsed = parseRegExp ( regexp )
31+ performance . mark ( 'parse-end' )
32+ performance . measure ( 'parse-duration' , 'parse-start' , 'parse-end' )
3333
34- console . log ( `time: ${ Math . round ( timeEnd - timeStart ) } ms` )
35- totalParseTime += timeEnd - timeStart
36- noError ++
3734 } catch ( e ) {
38- // console.error(new RegExp(regex, flags))
39- hasError ++
35+ console . error ( new RegExp ( regex , flags ) )
36+ parseErrorCount ++
4037 }
4138}
4239
43- console . log ( 'error ratio:' , hasError , '/' , hasError + noError )
40+ const totalParseTime = performance . getEntriesByName ( 'parse-duration' )
41+ . map ( entry => entry . duration )
42+ . reduce ( ( acc , d ) => acc + d , 0 )
43+
44+ const maxParseTime = performance . getEntriesByName ( 'parse-duration' )
45+ . map ( entry => entry . duration )
46+ . reduce ( ( acc , d ) => Math . max ( acc , d ) , - Infinity )
47+
48+ console . log ( 'error ratio:' , parseErrorCount , '/' , dataset . length )
4449console . log ( 'total parse time:' , Math . round ( totalParseTime ) , 'ms' )
50+ console . log ( 'avg parse time:' , Math . round ( totalParseTime / ( dataset . length - parseErrorCount ) ) , 'ms' )
51+ console . log ( 'max parse time:' , Math . round ( maxParseTime ) , 'ms' )
4552
0 commit comments