1+ var sf = require ( 'scarletsframe' ) ;
2+ var $ = sf . dom ;
3+
4+ // Declare variable for the model
5+ sf . model . for ( 'bench-mark' , function ( self ) {
6+ self . list = [ ] ;
7+ self . selected = - 1 ;
8+ // window.test = self;
9+ } ) ;
10+
11+ // Declare functions that controlling the model
12+ sf . controller . run ( 'bench-mark' , function ( self , root ) {
13+ var Measurer = root ( 'measurer' ) ;
14+
15+ var adjectives = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" , "elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" , "cheap" , "expensive" , "fancy" ] ;
16+ var colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
17+ var nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
18+
19+ var nextId = 1 ;
20+
21+ function _random ( max ) {
22+ return Math . round ( Math . random ( ) * 1000 ) % max ;
23+ }
24+
25+ self . buildData = function ( count = 1000 ) {
26+ var data = [ ] ;
27+ for ( var i = 0 ; i < count ; i ++ )
28+ data . push ( {
29+ id : nextId ++ ,
30+ isSelected :false ,
31+ label : adjectives [ _random ( adjectives . length ) ] + " " + colours [ _random ( colours . length ) ] + " " + nouns [ _random ( nouns . length ) ]
32+ } ) ;
33+
34+ return data ;
35+ }
36+
37+ self . unselect = function ( ) {
38+ if ( self . selected === - 1 ) return ;
39+
40+ if ( self . list [ self . selected ] !== undefined ) {
41+ self . list [ self . selected ] . isSelected = false ;
42+ self . list . refresh ( self . selected ) ;
43+ }
44+
45+ self . selected = - 1 ;
46+ }
47+
48+ // Handle button
49+ self . b_run = function ( ) {
50+ // Measurer.start("run");
51+ self . list = self . buildData ( ) ;
52+ self . selected = - 1 ;
53+ // Measurer.stop();
54+ }
55+
56+ self . b_runlots = function ( ) {
57+ // Measurer.start("runLots");
58+ self . list = self . buildData ( 10000 ) ;
59+ self . selected = - 1 ;
60+ // Measurer.stop();
61+ }
62+
63+ self . b_add = function ( ) {
64+ // Measurer.start("add");
65+ self . list = self . list . concat ( self . buildData ( 1000 ) ) ;
66+ // Measurer.stop();
67+ }
68+
69+ self . b_update = function ( ) {
70+ // Measurer.start("update");
71+ for ( var i = 0 ; i < self . list . length ; i += 10 ) {
72+ self . list [ i ] . label += ' !!!' ;
73+ self . list . refresh ( i , 'label' ) ;
74+ }
75+ // Measurer.stop();
76+ }
77+
78+ self . b_clear = function ( ) {
79+ // Measurer.start("clear");
80+ self . list . splice ( 0 ) ;
81+ self . selected = - 1 ;
82+ // Measurer.stop();
83+ }
84+
85+ self . b_swaprows = function ( ) {
86+ // Measurer.start("swapRows");
87+
88+ if ( self . list . length > 998 )
89+ self . list . swap ( 1 , 998 ) ;
90+
91+ // Measurer.stop();
92+ }
93+
94+ self . b_select = function ( el ) {
95+ // Measurer.start("select");
96+ self . unselect ( ) ;
97+
98+ var rowIndex = $ . parent ( el , '[sf-bind-list]' ) ;
99+ self . selected = rowIndex = sf . model . index ( rowIndex ) ;
100+
101+ self . list [ rowIndex ] . isSelected = true ;
102+ self . list . refresh ( rowIndex ) ;
103+ // Measurer.stop();
104+ }
105+
106+ self . b_remove = function ( el ) {
107+ // Measurer.start("delete");
108+
109+ var rowIndex = $ . parent ( el , '[sf-bind-list]' ) ;
110+ rowIndex = sf . model . index ( rowIndex ) ;
111+
112+ self . list . splice ( rowIndex , 1 ) ;
113+
114+ if ( rowIndex === self . selected )
115+ self . selected = - 1 ;
116+
117+ // Measurer.stop();
118+ }
119+ } ) ;
120+
121+ // Declare measure function in different scope
122+ sf . controller . run ( 'measurer' , function ( self ) {
123+ var startTime ;
124+ var lastMeasure ;
125+
126+ self . start = function ( name ) {
127+ startTime = performance . now ( ) ;
128+ lastMeasure = name ;
129+ }
130+ self . stop = function ( ) {
131+ var last = lastMeasure ;
132+ if ( lastMeasure ) {
133+ window . setTimeout ( function ( ) {
134+ lastMeasure = null ;
135+ var stop = performance . now ( ) ;
136+ var duration = 0 ;
137+ console . log ( last + " took " + ( stop - startTime ) ) ;
138+ } , 0 ) ;
139+ }
140+ }
141+ } ) ;
142+
143+ // We're not using dynamic resource loader
144+ sf . loader . off ( ) ;
0 commit comments