Another Cache question or two

20 views
Skip to first unread message

Bruce Axtens

unread,
Nov 24, 2022, 2:14:09 AM11/24/22
to Google Apps Script Community
Is CacheService faster or slower than PropertiesService?

Is there a window into the cache the way there is in the script editor for script properties?

-- Bruce

Andrew Roberts

unread,
Nov 24, 2022, 3:29:16 AM11/24/22
to google-apps-sc...@googlegroups.com
Is CacheService faster or slower than PropertiesService?

I don't know. Do some tests! I'd be interested to hear the results. You would assume CacheService is faster. This is the kind of thing that Tanaike is always doing, although I couldn't find anything on his site.

You've piqued my curiosity now! And there doesn't seem to be much in it:

function test() {
const cache = CacheService.getScriptCache()
cache.put('ctest', 1)
let now = new Date()
for (let i = 0; i < 100; i++) {
cache.get('ctest')
}
console.log(new Date().getTime() - now.getTime()) // 4026ms, 2856ms, 4161ms
}
 
function test2() {
const prop = PropertiesService.getScriptProperties()
prop.setProperty('ptest', 1)
let now = new Date()
for (let i = 0; i < 100; i++) {
prop.getProperty('ctest')
}
console.log(new Date().getTime() - now.getTime()) // 3512ms, 3623ms, 6197ms
}

Is there a window into the cache the way there is in the script editor for script properties?

No.
 

-- Bruce

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/3c58d56b-3d6f-4c2e-99ef-968afc59e92an%40googlegroups.com.

Andrew Roberts

unread,
Nov 24, 2022, 3:31:26 AM11/24/22
to google-apps-sc...@googlegroups.com
Just noticed my typo. But it didn't make any difference to the times.

Paul Armstrong

unread,
Nov 24, 2022, 8:17:08 PM11/24/22
to Google Apps Script Community
I vaguely recall someone doing some testing. But I can not find it now. I had thought that cached piped properties. But technically, I can not see why. They do the same thing with the same type of data (strings).

I also use an in-memory cache which I store at the same time I cache an object. That is, I have a JSON array indexed by the key, e.g., something like:
var cache = {};
cache[key] = {data};
 
It's only useful for the current call stack. It is gone between server calls. But if you are hitting the cache more than once in a single server-side call, it reduces the time from 50ms to 2ms, which is a non-trivial improvement. 
Reply all
Reply to author
Forward
0 new messages