@@ -3,38 +3,38 @@ import { bench, describe } from "vitest";
33import { compile , evaluate , register } from "../dist/index.esm.js" ;
44
55const context = {
6- user : {
7- name : "John" ,
8- age : 30 ,
9- isAdmin : true ,
10- scores : [ 85 , 90 , 78 , 92 ] ,
11- address : {
12- city : "New York" ,
13- zip : "10001" ,
14- } ,
15- } ,
16- products : [
17- { id : 1 , name : "Laptop" , price : 1200 } ,
18- { id : 2 , name : "Phone" , price : 800 } ,
19- { id : 3 , name : "Tablet" , price : 500 } ,
20- ] ,
21- calculateTotal : ( items : any [ ] ) => {
22- const total = items . reduce ( ( sum , item ) => sum + item . price , 0 ) ;
23- return total ;
24- } ,
25- applyDiscount : ( total : number , percentage : number ) => {
26- const value = total * ( 1 - percentage / 100 ) ;
27- return value ;
28- } ,
6+ user : {
7+ name : "John" ,
8+ age : 30 ,
9+ isAdmin : true ,
10+ scores : [ 85 , 90 , 78 , 92 ] ,
11+ address : {
12+ city : "New York" ,
13+ zip : "10001" ,
14+ } ,
15+ } ,
16+ products : [
17+ { id : 1 , name : "Laptop" , price : 1200 } ,
18+ { id : 2 , name : "Phone" , price : 800 } ,
19+ { id : 3 , name : "Tablet" , price : 500 } ,
20+ ] ,
21+ calculateTotal : ( items : any [ ] ) => {
22+ const total = items . reduce ( ( sum , item ) => sum + item . price , 0 ) ;
23+ return total ;
24+ } ,
25+ applyDiscount : ( total : number , percentage : number ) => {
26+ const value = total * ( 1 - percentage / 100 ) ;
27+ return value ;
28+ } ,
2929} ;
3030
3131const simpleExpression = "user.age + 5" ;
3232const mediumExpression = 'user.scores[2] > 80 ? "Good" : "Needs improvement"' ;
3333const complexExpression =
34- '@applyDiscount(@calculateTotal(products), 10) > 2000 ? "High value" : "Standard"' ;
34+ '@applyDiscount(@calculateTotal(products), 10) > 2000 ? "High value" : "Standard"' ;
3535
3636const complexExpression2 =
37- 'applyDiscount(calculateTotal(products), 10) > 2000 ? "High value" : "Standard"' ;
37+ 'applyDiscount(calculateTotal(products), 10) > 2000 ? "High value" : "Standard"' ;
3838
3939const simpleExpressionCompiler = compile ( simpleExpression ) ;
4040const mediumExpressionCompiler = compile ( mediumExpression ) ;
@@ -48,80 +48,80 @@ parser.functions.calculateTotal = context.calculateTotal;
4848parser . functions . applyDiscount = context . applyDiscount ;
4949
5050const newFunctionSimple = new Function (
51- "context" ,
52- `with(context) { return ${ simpleExpression } ; }` ,
51+ "context" ,
52+ `with(context) { return ${ simpleExpression } ; }` ,
5353) ;
5454const newFunctionMedium = new Function (
55- "context" ,
56- `with(context) { return ${ mediumExpression } ; }` ,
55+ "context" ,
56+ `with(context) { return ${ mediumExpression } ; }` ,
5757) ;
5858const newFunctionComplex = new Function (
59- "context" ,
60- `with(context) { return ${ complexExpression2 } ; }` ,
59+ "context" ,
60+ `with(context) { return ${ complexExpression2 } ; }` ,
6161) ;
6262
6363describe ( "Simple Expression Benchmarks" , ( ) => {
64- bench ( "evaluate after compile (baseline) only interpreter" , ( ) => {
65- simpleExpressionCompiler ( context ) ;
66- } ) ;
67-
68- bench ( "new Function (vs evaluate)" , ( ) => {
69- newFunctionSimple ( context ) ;
70- } ) ;
71-
72- bench (
73- "evaluate without compile (vs evaluate) tokenize + parse + interpreter" ,
74- ( ) => {
75- evaluate ( simpleExpression , context ) ;
76- } ,
77- ) ;
78-
79- bench ( "expr-eval Parser (vs evaluate)" , ( ) => {
80- // @ts -ignore
81- Parser . evaluate ( simpleExpression , context ) ;
82- } ) ;
64+ bench ( "evaluate after compile (baseline) only interpreter" , ( ) => {
65+ simpleExpressionCompiler ( context ) ;
66+ } ) ;
67+
68+ bench ( "new Function (vs evaluate)" , ( ) => {
69+ newFunctionSimple ( context ) ;
70+ } ) ;
71+
72+ bench (
73+ "evaluate without compile (vs evaluate) tokenize + parse + interpreter" ,
74+ ( ) => {
75+ evaluate ( simpleExpression , context ) ;
76+ } ,
77+ ) ;
78+
79+ bench ( "expr-eval Parser (vs evaluate)" , ( ) => {
80+ // @ts -ignore
81+ Parser . evaluate ( simpleExpression , context ) ;
82+ } ) ;
8383} ) ;
8484
8585describe ( "Medium Expression Benchmarks" , ( ) => {
86- bench ( "evaluate after compile (baseline) only interpreter" , ( ) => {
87- mediumExpressionCompiler ( context ) ;
88- } ) ;
89-
90- bench ( "new Function (vs evaluate)" , ( ) => {
91- newFunctionMedium ( context ) ;
92- } ) ;
93-
94- bench (
95- "evaluate without compile (vs evaluate) tokenize + parse + interpreter" ,
96- ( ) => {
97- evaluate ( mediumExpression , context ) ;
98- } ,
99- ) ;
100-
101- bench ( "expr-eval Parser (vs evaluate)" , ( ) => {
102- // @ts -ignore
103- Parser . evaluate ( mediumExpression , context ) ;
104- } ) ;
86+ bench ( "evaluate after compile (baseline) only interpreter" , ( ) => {
87+ mediumExpressionCompiler ( context ) ;
88+ } ) ;
89+
90+ bench ( "new Function (vs evaluate)" , ( ) => {
91+ newFunctionMedium ( context ) ;
92+ } ) ;
93+
94+ bench (
95+ "evaluate without compile (vs evaluate) tokenize + parse + interpreter" ,
96+ ( ) => {
97+ evaluate ( mediumExpression , context ) ;
98+ } ,
99+ ) ;
100+
101+ bench ( "expr-eval Parser (vs evaluate)" , ( ) => {
102+ // @ts -ignore
103+ Parser . evaluate ( mediumExpression , context ) ;
104+ } ) ;
105105} ) ;
106106
107107describe ( "Complex Expression Benchmarks" , ( ) => {
108- bench ( "evaluate after compile (baseline) only interpreter" , ( ) => {
109- complexExpressionCompiler ( context ) ;
110- } ) ;
111-
112- bench ( "new Function (vs evaluate)" , ( ) => {
113- newFunctionComplex ( context ) ;
114- } ) ;
115-
116- bench (
117- "evaluate without compile (vs evaluate) tokenize + parse + interpreter" ,
118- ( ) => {
119- evaluate ( complexExpression2 , context ) ;
120- } ,
121- ) ;
122-
123- bench ( "expr-eval Parser (vs evaluate)" , ( ) => {
124- // @ts -ignore
125- parser . evaluate ( complexExpression2 , context ) ;
126- } ) ;
108+ bench ( "evaluate after compile (baseline) only interpreter" , ( ) => {
109+ complexExpressionCompiler ( context ) ;
110+ } ) ;
111+
112+ bench ( "new Function (vs evaluate)" , ( ) => {
113+ newFunctionComplex ( context ) ;
114+ } ) ;
115+
116+ bench (
117+ "evaluate without compile (vs evaluate) tokenize + parse + interpreter" ,
118+ ( ) => {
119+ evaluate ( complexExpression2 , context ) ;
120+ } ,
121+ ) ;
122+
123+ bench ( "expr-eval Parser (vs evaluate)" , ( ) => {
124+ // @ts -ignore
125+ parser . evaluate ( complexExpression2 , context ) ;
126+ } ) ;
127127} ) ;
0 commit comments