问题
Of the two choices I have to access the value of a control which is the most efficient?
getComponent("ControlName").getValue();
or
dataSource.getItemValue("FieldName");
I find that on occasion the getComponent
does not seem to return the current value, but accessing the dataSource
seems to be more reliable. So does it make much difference from a performance perspective which one is used?
The dataSource.getValue seems to work everywhere that I have tried it. However, when working with rowData I still seem to need to do a rowData.getColumnValue("Something"). rowData.getValue("Something") fails.
回答1:
Neither. The fastest syntax is dataSource.getValue ("FieldName")
. The getItemValue
method is only reliable on the document data source, whereas the getValue
method is not only also available on view entries accessed via a view data source (although in that context you would pass it the programmatic name of a view column, which is not necessarily the same name as a field), but will also be available on any custom data sources that you develop or install (e.g. third-party extension libraries). Furthermore, it does automatic type conversion that you'd have to do yourself if you used getItemValue
instead.
Even on very simple pages, dataSource.getValue ("FieldName")
is 5 times as fast as getComponent ("id").getValue ()
, because, as Fredrik mentions, first it has to find the component, and then ask it what the value is... which, behind the scenes, just asks the data source anyway. So it will always be faster to just ask the data source yourself.
NOTE: the corresponding write method is dataSource.setValue ("FieldName", "NewValue")
, not dataSource.replaceItemValue ("FieldName", "NewValue")
. Both will work, but setValue
also does the same type conversion that getValue
does, so you can pass it data that doesn't strictly conform to the old Domino Java API and it usually just figures out what the value needs to be converted to in order to be "safe" for Domino to store.
回答2:
I would say that the most efficient way is to get the value directly from the datasource. Because if you use getComponent("ControlName").getValue(); you will do a get on the component first and then a getValue from that. So do a single get from the datasource is more efficient if you ask me.
来源:https://stackoverflow.com/questions/18303862/which-is-the-most-efficient-way-to-access-the-value-of-a-control