1# Recommendations for Improving Performance 2 3Poor-performing code may work, but will take away from your application performance. This topic presents a line-up of recommendations that you can take to improve your implementation, thereby avoiding possible performance drop. 4 5## Using Lazy Loading 6 7When developing a long list, use of loop rendering, as in the code snippet below, can greatly slow down page loading and increase server load. 8 9```ts 10@Entry 11@Component 12struct MyComponent { 13 @State arr: number[] = Array.from(Array(100), (v,k) =>k); // Construct an array of 0 to 99. 14 build() { 15 List() { 16 ForEach(this.arr, (item: number) => { 17 ListItem() { 18 Text(`item value: ${item}`) 19 } 20 }, (item: number) => item.toString()) 21 } 22 } 23} 24``` 25 26The preceding code snippet loads all of the 100 list elements at a time during page loading. This is generally not desirable. Instead, what we need is to load data from the data source and create corresponding components on demand. This can be achieved through lazy loading. The sample code is as follows: 27 28```ts 29class BasicDataSource implements IDataSource { 30 private listeners: DataChangeListener[] = [] 31 32 public totalCount(): number { 33 return 0 34 } 35 36 public getData(index: number): any { 37 return undefined 38 } 39 40 registerDataChangeListener(listener: DataChangeListener): void { 41 if (this.listeners.indexOf(listener) < 0) { 42 console.info('add listener') 43 this.listeners.push(listener) 44 } 45 } 46 47 unregisterDataChangeListener(listener: DataChangeListener): void { 48 const pos = this.listeners.indexOf(listener); 49 if (pos >= 0) { 50 console.info('remove listener') 51 this.listeners.splice(pos, 1) 52 } 53 } 54 55 notifyDataReload(): void { 56 this.listeners.forEach(listener => { 57 listener.onDataReloaded() 58 }) 59 } 60 61 notifyDataAdd(index: number): void { 62 this.listeners.forEach(listener => { 63 listener.onDataAdd(index) 64 }) 65 } 66 67 notifyDataChange(index: number): void { 68 this.listeners.forEach(listener => { 69 listener.onDataChange(index) 70 }) 71 } 72 73 notifyDataDelete(index: number): void { 74 this.listeners.forEach(listener => { 75 listener.onDataDelete(index) 76 }) 77 } 78 79 notifyDataMove(from: number, to: number): void { 80 this.listeners.forEach(listener => { 81 listener.onDataMove(from, to) 82 }) 83 } 84} 85 86class MyDataSource extends BasicDataSource { 87 private dataArray: string[] = ['item value: 0', 'item value: 1', 'item value: 2'] 88 89 public totalCount(): number { 90 return this.dataArray.length 91 } 92 93 public getData(index: number): any { 94 return this.dataArray[index] 95 } 96 97 public addData(index: number, data: string): void { 98 this.dataArray.splice(index, 0, data) 99 this.notifyDataAdd(index) 100 } 101 102 public pushData(data: string): void { 103 this.dataArray.push(data) 104 this.notifyDataAdd(this.dataArray.length - 1) 105 } 106} 107 108@Entry 109@Component 110struct MyComponent { 111 private data: MyDataSource = new MyDataSource() 112 113 build() { 114 List() { 115 LazyForEach(this.data, (item: string) => { 116 ListItem() { 117 Row() { 118 Text(item).fontSize(20).margin({ left: 10 }) 119 } 120 } 121 .onClick(() => { 122 this.data.pushData('item value: ' + this.data.totalCount()) 123 }) 124 }, item => item) 125 } 126 } 127} 128``` 129 130 131 132The preceding code initializes only three list elements during page loading and loads a new list item each time a list element is clicked. 133 134## Setting Width and Height for \<List> Components 135 136When a **\<List>** component is nested within a **\<Scroll>** component, all of its content will be loaded if its width and height is not specified, which may result in performance drop. 137 138> **NOTE** 139> 140> When a **\<List>** component is nested within a **\<Scroll>** component: 141> 142> - If the width and height of the **\<List>** component are not set, all its child components are laid out. 143> 144> - If the width and height of the **\<List>** component are set, only child components within its display area are laid out. 145> 146> - When [ForEach](../quick-start/arkts-rendering-control-foreach.md) is used to load child components in the **\<List>** component, all child components are laid out, regardless of whether the width and height are set. 147> 148> - When [LazyForEach](../quick-start/arkts-rendering-control-lazyforeach.md) is used to load child components in the **\<List>** component, all child components are laid out if the component does not have its width and height specified; and only child components within its display area are laid out if the component has its width and height specified. 149 150```ts 151class BasicDataSource implements IDataSource { 152 private listeners: DataChangeListener[] = [] 153 154 public totalCount(): number { 155 return 0 156 } 157 158 public getData(index: number): any { 159 return undefined 160 } 161 162 registerDataChangeListener(listener: DataChangeListener): void { 163 if (this.listeners.indexOf(listener) < 0) { 164 console.info('add listener') 165 this.listeners.push(listener) 166 } 167 } 168 169 unregisterDataChangeListener(listener: DataChangeListener): void { 170 const pos = this.listeners.indexOf(listener); 171 if (pos >= 0) { 172 console.info('remove listener') 173 this.listeners.splice(pos, 1) 174 } 175 } 176 177 notifyDataReload(): void { 178 this.listeners.forEach(listener => { 179 listener.onDataReloaded() 180 }) 181 } 182 183 notifyDataAdd(index: number): void { 184 this.listeners.forEach(listener => { 185 listener.onDataAdd(index) 186 }) 187 } 188 189 notifyDataChange(index: number): void { 190 this.listeners.forEach(listener => { 191 listener.onDataChange(index) 192 }) 193 } 194 195 notifyDataDelete(index: number): void { 196 this.listeners.forEach(listener => { 197 listener.onDataDelete(index) 198 }) 199 } 200 201 notifyDataMove(from: number, to: number): void { 202 this.listeners.forEach(listener => { 203 listener.onDataMove(from, to) 204 }) 205 } 206} 207 208class MyDataSource extends BasicDataSource { 209 private dataArray: Array<string> = new Array(100).fill('test') 210 211 public totalCount(): number { 212 return this.dataArray.length 213 } 214 215 public getData(index: number): any { 216 return this.dataArray[index] 217 } 218 219 public addData(index: number, data: string): void { 220 this.dataArray.splice(index, 0, data) 221 this.notifyDataAdd(index) 222 } 223 224 public pushData(data: string): void { 225 this.dataArray.push(data) 226 this.notifyDataAdd(this.dataArray.length - 1) 227 } 228} 229 230@Entry 231@Component 232struct MyComponent { 233 private data: MyDataSource = new MyDataSource() 234 235 build() { 236 Scroll() { 237 List() { 238 LazyForEach(this.data, (item: string, index: number) => { 239 ListItem() { 240 Row() { 241 Text('item value: ' + item + (index + 1)).fontSize(20).margin(10) 242 } 243 } 244 }) 245 } 246 } 247 } 248} 249``` 250 251In the above scenario, you are advised to set the width and height for the **\<List>** component. 252 253```ts 254class BasicDataSource implements IDataSource { 255 private listeners: DataChangeListener[] = [] 256 257 public totalCount(): number { 258 return 0 259 } 260 261 public getData(index: number): any { 262 return undefined 263 } 264 265 registerDataChangeListener(listener: DataChangeListener): void { 266 if (this.listeners.indexOf(listener) < 0) { 267 console.info('add listener') 268 this.listeners.push(listener) 269 } 270 } 271 272 unregisterDataChangeListener(listener: DataChangeListener): void { 273 const pos = this.listeners.indexOf(listener); 274 if (pos >= 0) { 275 console.info('remove listener') 276 this.listeners.splice(pos, 1) 277 } 278 } 279 280 notifyDataReload(): void { 281 this.listeners.forEach(listener => { 282 listener.onDataReloaded() 283 }) 284 } 285 286 notifyDataAdd(index: number): void { 287 this.listeners.forEach(listener => { 288 listener.onDataAdd(index) 289 }) 290 } 291 292 notifyDataChange(index: number): void { 293 this.listeners.forEach(listener => { 294 listener.onDataChange(index) 295 }) 296 } 297 298 notifyDataDelete(index: number): void { 299 this.listeners.forEach(listener => { 300 listener.onDataDelete(index) 301 }) 302 } 303 304 notifyDataMove(from: number, to: number): void { 305 this.listeners.forEach(listener => { 306 listener.onDataMove(from, to) 307 }) 308 } 309} 310 311class MyDataSource extends BasicDataSource { 312 private dataArray: Array<string> = new Array(100).fill('test') 313 314 public totalCount(): number { 315 return this.dataArray.length 316 } 317 318 public getData(index: number): any { 319 return this.dataArray[index] 320 } 321 322 public addData(index: number, data: string): void { 323 this.dataArray.splice(index, 0, data) 324 this.notifyDataAdd(index) 325 } 326 327 public pushData(data: string): void { 328 this.dataArray.push(data) 329 this.notifyDataAdd(this.dataArray.length - 1) 330 } 331} 332 333@Entry 334@Component 335struct MyComponent { 336 private data: MyDataSource = new MyDataSource() 337 338 build() { 339 Scroll() { 340 List() { 341 LazyForEach(this.data, (item: string, index: number) => { 342 ListItem() { 343 Text('item value: ' + item + (index + 1)).fontSize(20).margin(10) 344 }.width('100%') 345 }) 346 }.width('100%').height(500) 347 }.backgroundColor(Color.Pink) 348 } 349} 350``` 351 352 353 354## Prioritizing Conditional Rendering over Visibility Control 355 356Use of the visibility attribute to hide or show a component, as in the code snippet below, results in re-creation of the component, leading to performance drop. 357 358```ts 359@Entry 360@Component 361struct MyComponent { 362 @State isVisible: Visibility = Visibility.Visible; 363 364 build() { 365 Column() { 366 Button ("Show/Hide") 367 .onClick(() => { 368 if (this.isVisible == Visibility.Visible) { 369 this.isVisible = Visibility.None 370 } else { 371 this.isVisible = Visibility.Visible 372 } 373 }) 374 Row().visibility(this.isVisible) 375 .width(300).height(300).backgroundColor(Color.Pink) 376 }.width('100%') 377 } 378} 379``` 380 381To avoid the preceding issue, use the **if** statement instead. The sample code is as follows: 382 383```ts 384@Entry 385@Component 386struct MyComponent { 387 @State isVisible: boolean = true; 388 389 build() { 390 Column() { 391 Button ("Show/Hide") 392 .onClick(() => { 393 this.isVisible = !this.isVisible 394 }) 395 if (this.isVisible) { 396 Row() 397 .width(300).height(300).backgroundColor(Color.Pink) 398 } 399 }.width('100%') 400 } 401} 402``` 403 404 405 406## Prioritizing Flex over Column/Row 407 408By default, the flex container needs to re-lay out flex items to comply with the **flexShrink** and **flexGrow** settings. This may result in drop in rendering performance. 409 410```ts 411@Entry 412@Component 413struct MyComponent { 414 build() { 415 Flex({ direction: FlexDirection.Column }) { 416 Flex().width(300).height(200).backgroundColor(Color.Pink) 417 Flex().width(300).height(200).backgroundColor(Color.Yellow) 418 Flex().width(300).height(200).backgroundColor(Color.Grey) 419 } 420 } 421} 422``` 423 424To avoid the preceding issue, replace **Flex** with **Column** and **Row**, which can create the same page layout as **Flex** does. 425 426```ts 427@Entry 428@Component 429struct MyComponent { 430 build() { 431 Column() { 432 Row().width(300).height(200).backgroundColor(Color.Pink) 433 Row().width(300).height(200).backgroundColor(Color.Yellow) 434 Row().width(300).height(200).backgroundColor(Color.Grey) 435 } 436 } 437} 438``` 439 440 441 442## Minimizing White Blocks During Swiping 443 444To minimize white blocks during swiping, expand the UI loading range by increasing the value of **cachedCount** for the **\<List>** and **\<Grid>** components. **cachedCount** indicates the number of list or grid items preloaded outside of the screen. 445If an item needs to request an online image, set **cachedCount** as appropriate so that the image is downloaded in advance before the item comes into view on the screen, thereby reducing the number of white blocks. 446The following is an example of using **cachedCount**: 447 448```ts 449@Entry 450@Component 451struct MyComponent { 452 private source: MyDataSource = new MyDataSource(); 453 454 build() { 455 List() { 456 LazyForEach(this.source, item => { 457 ListItem() { 458 Text("Hello" + item) 459 .fontSize(50) 460 .onAppear(() => { 461 console.log("appear:" + item) 462 }) 463 } 464 }) 465 }.cachedCount(3) // Increase the number of list or grid items preloaded outside of the screen. 466 } 467} 468 469class MyDataSource implements IDataSource { 470 data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; 471 472 public totalCount(): number { 473 return this.data.length 474 } 475 476 public getData(index: number): any { 477 return this.data[index] 478 } 479 480 registerDataChangeListener(listener: DataChangeListener): void { 481 } 482 483 unregisterDataChangeListener(listener: DataChangeListener): void { 484 } 485} 486``` 487 488 489**Instructions** 490A greater **cachedCount** value may result in higher CPU and memory overhead of the UI. Adjust the value by taking into account both the comprehensive performance and user experience. 491