I have used memoized functions to act as a cache i...
# suitescript
s
I have used memoized functions to act as a cache in SS 1.0 scripts
c
Do you have a link to something that describes this? Would a Google search work?
s
it should. make sure to search for somethign like memoization javascript or memoized functions javascript, as the technique is used in other functional languages as well
I just build my own, but you could use a library. Here is an example that caches Account internal ids by account number:
1000 1
c
doesn't cache get re-initialized every time?
s
well, yes. it's only as good as a global variable, but doesn't pollute the global space. but if you have to do the same lookup multiple times in a single script, it can reduce governance usage and speed up runtime. it won't persist across multiple script executions
c
gotcha
globals didn't persist in server-side scripts either if i remember correctly in 1.0 either
s
I use this in my GL plug-ins because many line items in a transaction use the same account number, so I end up needing the same result multiple times. It noticeably improved the time to create the custom GL lines, which is the real reason I used it (governance wasn't a concern)
c
Yeah not a bad usage its definitely a valid way
s
And of course because GL plug-ins still only support SS 1.0 😠
c
yeah its a weird plugin all together but not that bad
b
you might just want to get all the accounts beforehand.
usually there arent that many
s
We have quite a few, unfortunately
b
you can also probably gather all the account numbers beforehand and do 1 search
s
Yeah, that is reasonable too, but we have over 500 accounts, and the GL impact of our cash sale items only need 12 of them
b
as in you write a getAccountIds function that takes an array of alll the account ids you want to find
then do 1 search to get all of them instead of 1 search for each of them
s
Yes, I thought of that idea as well.
But I had already written it this way and stuffed it inside a Utility library. It works, so I will probably just leave it alone. Because of the nature of our cash sales, there are only ever going to be two searches executed, so it's not much worse than a single search.
If that were not the case, then it would definitely be more efficient to get them all at once.
b
memorization on a search tends to be wasteful. you dont really want to run a search for each value. you want to run 1 search for all your values.
s
I agree with you. This is not the best use case for this technique, it was just the shortest example I have in code, so I thought it would be easiest to extrapolate from. Memoization works best for any slow or computationally expensive functions, so that you don't incur the time cost repeatedly for the same inputs.
But you use more memory to save time, so even that is a trade-off to consider.
Back when I used to write SOQL queries in Apex (Salesforce), the best practice was to loop through a collection to get all values and run a single query for all of them at once. I think it's a good idea for any multi-tenant system with governance, generally. But I also feel that 2 searches are not much worse than 1. Once it get to 3 or more of the same search, then I would rewrite it to perform a single search instead.
c
thats just good in general i'd say
s
The history behind this is kind of interesting, actually. At the time we started writing it, there was a race condition with asynchronous GL plugins that could duplicate custom GL lines. To minimize the risk of that, we needed the plugin to run as fast as possible, which meant taking out all of the searches and having hard-coded object maps of results. Eventually NetSuite fixed that bug, so we rewrote the lookup functions to use searches, and to achieve a little efficiency, we cached the results as we came across them. We could achieve that by just touching a few functions in one library file. Updating the code at that point to gather all of the account numbers and other search values up front would have been a bigger, riskier rewrite, and not really worth it for our use case. We definitely would not have architected it this way in the first place if it weren't for that initial bug.
c
This discussion is fantastic.
Thanks for everyone's input!