Created
June 25, 2012 15:37
-
-
Save pec1985/2989311 to your computer and use it in GitHub Desktop.
Optimizing Appcelerator's Titanium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test #1 | |
* Adding 10,000 Ti.UI.TableViewRows to a Ti.UI.TableView | |
* The rows have the Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE constant in them | |
* | |
* Total time in my Mac: 1228ms | |
*/ | |
var win = Ti.UI.createWindow({ | |
backgroundColor: '#ccc' | |
}); | |
win.open(); | |
var table = Ti.UI.createTableView(); | |
win.add(table); | |
// Start the timer | |
var time = new Date().getTime(); | |
// ========================================== | |
var data = []; | |
for(var i = 0; i < 10000; i++){ | |
data.push( | |
Ti.UI.createTableViewRow({ | |
title:'Row #'+i, | |
selectionStyle: Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE | |
}) | |
); | |
} | |
table.setData(data); | |
// ========================================== | |
// End the timer | |
var end = new Date().getTime(); | |
Ti.API.info('It took ' + (end - time)+'ms'); | |
// 1228ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test #2 | |
* Adding 10,000 Ti.UI.TableViewRows to a Ti.UI.TableView | |
* A JavaScript variable holds the Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE constant | |
* That variable is then passed to the rows, this way we don't call Ti every time | |
* | |
* Total time in my Mac: 826ms | |
*/ | |
var win = Ti.UI.createWindow({ | |
backgroundColor: '#ccc' | |
}); | |
win.open(); | |
var table = Ti.UI.createTableView(); | |
win.add(table); | |
var SELECTION_STYLE_BLUE = Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE; | |
// Start the timer | |
var time = new Date().getTime(); | |
// ========================================== | |
var data = []; | |
for(var i = 0; i < 10000; i++){ | |
data.push( | |
Ti.UI.createTableViewRow({ | |
title:'Row #'+i, | |
selectionStyle: SELECTION_STYLE_BLUE | |
}) | |
); | |
} | |
table.setData(data); | |
// ========================================== | |
// End the timer | |
var end = new Date().getTime(); | |
Ti.API.info('It took ' + (end - time)+'ms'); | |
// 826ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test #3 | |
* Adding 10,000 JavaScript Objects to a TableView | |
* A JavaScript variable holds the Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE constant | |
* That variable is then passed to the Object, this way we don't call Ti every time | |
* In this test, I have an object instead of a Ti.UI.TableViewRow | |
* | |
* Total time in my Mac: 460ms | |
*/ | |
var win = Ti.UI.createWindow({ | |
backgroundColor: '#ccc' | |
}); | |
win.open(); | |
var table = Ti.UI.createTableView(); | |
win.add(table); | |
var SELECTION_STYLE_BLUE = Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE; | |
// Start the timer | |
var time = new Date().getTime(); | |
// ========================================== | |
var data = []; | |
for(var i = 0; i < 10000; i++){ | |
data.push({ | |
title:'Row #'+i, | |
selectionStyle: SELECTION_STYLE_BLUE | |
}); | |
} | |
table.setData(data); | |
// ========================================== | |
// End the timer | |
var end = new Date().getTime(); | |
Ti.API.info('It took ' + (end - time)+'ms'); | |
// 460 |
I hope that you understand that doing this: var createRow = Ti.UI.createTableViewRow; can be problematic in JS.
I used similar optimizations when there was only Rhino available on Android, but I used this: var UI = Ti.UI;
That's safer to do and it brings some minor performance improvements, but now when we have v8, that kind of optimizations are useless on Android.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hrm. I need to look at this a bit closer. We have one app that creates a table with nearly 1000 rows which each have 2 labels. Wondering how much time I can chop off using freddy's findings.