6

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.

Bill F
  • 2,057
  • 3
  • 18
  • 39

2 Answers2

17

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.

Tim Tripcony
  • 8,056
  • 1
  • 23
  • 34
  • Thanks, this is really important to know I have an XPage that I do this sort of lookup. Now back to the code and find all those places that I use this and change them. This is a post that I have copied into my cheatsheet document for future reference and reminder. Many times the most obvious method is far from the best method. – Bill F Aug 19 '13 at 15:18
  • Here is a bit of a followup. In a repeat control I display values from the rowData by binding a computedField to a fieled in the rowData. I'm guessing that a rowData.getValue would be faster? – Bill F Aug 19 '13 at 16:32
  • Ah - an invaluable (pun intended) best practise for development: never go after the backing element/thingi, stick with the data source. – stwissel Aug 20 '13 at 02:44
4

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.

Fredrik Norling
  • 3,461
  • 17
  • 21