@@ -22,7 +22,7 @@ const generateData = (size: number) => {
2222}
2323
2424// Test data - generate 1000 items but split into initial and incremental sets
25- const totalSize = 10000
25+ const totalSize = 50000
2626const initialSize = 9000
2727const incrementalRuns = totalSize - initialSize
2828const { users : allUsers , posts : allPosts } = generateData ( totalSize )
@@ -87,6 +87,69 @@ joinSuite.add({
8787 } ,
8888} )
8989
90+ // Add naive indexed join benchmark
91+ joinSuite . add ( {
92+ name : 'Naive Indexed Join' ,
93+ setup : ( ) => ( {
94+ currentUsers : [ ...initialUsers ] ,
95+ currentPosts : [ ...initialPosts ] ,
96+ postsByUser : new Map < number , typeof initialPosts > ( ) ,
97+ result : [ ] as { userName : string ; postTitle : string } [ ] ,
98+ } ) ,
99+ firstRun : ( ctx ) => {
100+ // Build post index
101+ ctx . postsByUser . clear ( )
102+ for ( const post of ctx . currentPosts ) {
103+ if ( ! ctx . postsByUser . has ( post . userId ) ) {
104+ ctx . postsByUser . set ( post . userId , [ ] )
105+ }
106+ ctx . postsByUser . get ( post . userId ) ! . push ( post )
107+ }
108+
109+ // Compute join using index
110+ ctx . result = [ ]
111+ for ( const user of ctx . currentUsers ) {
112+ const userPosts = ctx . postsByUser . get ( user . id ) || [ ]
113+ for ( const post of userPosts ) {
114+ ctx . result . push ( {
115+ userName : user . name ,
116+ postTitle : post . title ,
117+ } )
118+ }
119+ }
120+ } ,
121+ incrementalRun : ( ctx , i ) => {
122+ const user = incrementalUsers [ i ]
123+ const post1 = incrementalPosts [ i * 2 ]
124+ const post2 = incrementalPosts [ i * 2 + 1 ]
125+
126+ // Update data
127+ ctx . currentUsers . push ( user )
128+ ctx . currentPosts . push ( post1 , post2 )
129+
130+ // Rebuild post index from scratch
131+ ctx . postsByUser . clear ( )
132+ for ( const post of ctx . currentPosts ) {
133+ if ( ! ctx . postsByUser . has ( post . userId ) ) {
134+ ctx . postsByUser . set ( post . userId , [ ] )
135+ }
136+ ctx . postsByUser . get ( post . userId ) ! . push ( post )
137+ }
138+
139+ // Recompute entire join using index
140+ ctx . result = [ ]
141+ for ( const user of ctx . currentUsers ) {
142+ const userPosts = ctx . postsByUser . get ( user . id ) || [ ]
143+ for ( const post of userPosts ) {
144+ ctx . result . push ( {
145+ userName : user . name ,
146+ postTitle : post . title ,
147+ } )
148+ }
149+ }
150+ } ,
151+ } )
152+
90153// Add D2TS join benchmark
91154joinSuite . add ( {
92155 name : 'D2TS Join' ,
0 commit comments