Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon 使用getAllValues()而不是getValue(),是否会含义性能


使用getAllValues()而不是getValue(),是否会含义性能


在比较`getAllValues()`与`getValue()`时,在很大程度上取决于您正在使用的系统或框架中这些方法的上下文和特定实现。但是,有一些一般原则可以指导我们的理解:

##一般绩效注意事项

1。通话数:通常,检索数据的较少呼叫比拨打多个呼叫更有效。如果`getAllValues()`一个呼叫中检索所有必要的数据,则比调用每个值多次调用`getValue()更有效。

2。数据检索:如果`getallvalues()`获取比所需的更多数据,则由于数据传输和处理的增加而可能会慢得多。但是,如果您无论如何都需要所有的值,那么一次而不是单独获取它们可能会更有效。

3.缓存和优化:某些系统可能会缓存结果或优化对`getValue()`的重复调用,从而可以减轻性能差异。但是,如果`getAllValues()`是针对批量检索进行了优化的,那么它仍然可以提供更好的性能。

##特定方案

-Google Apps脚本:在Google Apps脚本中,使用`getValues()`(类似于`getAllValues()`)在多个``getValue()呼叫上建议使用``getValue()'',因为它可以减少与Google Sheets API的交互作用的数量,从而可以显着提高性能[3]。

- 反射和属性访问:在.NET反射中,由于反射本身的开销,诸如`getProperties()之类的方法可能会很慢。但是,缓存结果可以大大提高性能[1] [7]。

-ServiceNow:在ServiceNow中,`getValue()`通常比`getDisplayValue()`,因为它在没有其他处理的情况下检索原始值[5]。

## 结论

`getAllValues()和`getValue()之间的选择应基于应用程序的特定要求和您正在使用的系统的性能特征。如果您需要所有值,并且可以使用`getAllValues()````可能是更好的选择''有效地检索它们。但是,如果您仅需要一个值的子集或`getValue()`是针对用例优化的,则可能是可取的。在做出此决定时,始终考虑数据尺寸,网络延迟和特定于系统的优化。

引用:
[1] https://lotsacode.wordpress.com/2010/04/13/reflection-type-getproperties-and-performance/
[2] https://www.reddit.com/r/googleappsscript/comments/comments/px959t/getrangegetvalues_performance_implication_implication_when/
[3] https://stackoverflow.com/questions/42776311/optimizing-code-getValue-and-looping
[4] https://drupal.stackexchange.com/questions/214036/getValue-of-multiple-values-values-field-field-return-only-the-the-first-value
[5] https://www.servicenow.com/community/in-other-news/how-to-come-use-getdisplayvalue-and-getValue-for-gliderecord-or/ba-p/ba-p/2896719
[6] https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/page/app-store/dev_portal/api_reference/glide/glide/concept/conecte/c_glideformapi.html
[7] http://www.mattwarren.org/2016/12/14/why-is-reflection-slow/
[8] https://www.ag-grid.com/angular-data-grid/component-cell-renderer/
[9] https://salesforce.stackexchange.com/questions/8829/getInstance-vs-getValues-custom-custom-netting