File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { createElement } from '../src/createElement' ;
2+ import { diff } from '../src/diff' ;
3+
4+ function buildTree ( depth : number , breadth : number ) : any {
5+ if ( depth === 0 ) return createElement ( 'span' , { } , [ 'leaf' ] ) ;
6+ const children = Array . from ( { length : breadth } , ( ) => buildTree ( depth - 1 , breadth ) ) ;
7+ return createElement ( 'div' , { className : 'l' + depth } , children ) ;
8+ }
9+
10+ function bench ( name : string , fn : ( ) => void , n = 1000 ) {
11+ const start = performance . now ( ) ;
12+ for ( let i = 0 ; i < n ; i ++ ) fn ( ) ;
13+ const ms = ( ( performance . now ( ) - start ) / n ) . toFixed ( 3 ) ;
14+ console . log ( name + ': ' + ms + 'ms avg (' + n + ' runs)' ) ;
15+ }
16+
17+ const a = buildTree ( 3 , 3 ) ;
18+ const b = buildTree ( 3 , 3 ) ;
19+ bench ( 'diff small (3x3)' , ( ) => diff ( a , b ) ) ;
20+
21+ const ma = buildTree ( 4 , 4 ) ;
22+ const mb = buildTree ( 4 , 4 ) ;
23+ bench ( 'diff medium (4x4)' , ( ) => diff ( ma , mb ) ) ;
You can’t perform that action at this time.
0 commit comments