Performance difference between client GlideRecord and GlideAjax: Negligible
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 09:08 AM
I was part of a discussion about this this topic. The thread participants decided that GlideAjax should be favored over client-side GlideRecord, based on recommendations on the SN wiki. I decided to see for myself.
I created a GlideAjax script to fetch a manager:
function onLoad() { var ga = new GlideAjax("GMF_BenchmarkSample"); ga.addParam("sysparm_name", 'getManagerId'); ga.addParam("sysparm_userId", g_form.getValue('caller_id')); ga.getXML(callBack); } function callBack(response){ var answer = response.responseXML.documentElement.getAttribute("answer"); alert(answer); }
On the back end I used this script include:
var GMF_BenchmarkSample = Class.create(); GMF_BenchmarkSample.prototype = Object.extendsObject(AbstractAjaxProcessor, { //Log the error using server side error logging getManagerId: function () { var userId = this.getParameter('sysparm_userId'); var gr = new GlideRecord('sys_user'); if(gr.get(userId)){ return gr.manager;} return null; }, });
Then I created a GlideRecord version to do the same thing:
function onLoad() { var rec = new GlideRecord('sys_user'); rec.addQuery('sys_id', g_form.getValue('caller_id')); rec.query(recResponse); } function recResponse(rec) { while (rec.next()) { alert(rec.manager); } }
Based on the advice on the wiki, the GlideRecord version should perform worse ostensibly due to having to load all of the properties on the user record. However I found this:
Since the client GlideRecord is considerably easier to work with and only causes a negligible performance hit, I'm switching to using it in the future.
(The test was executed multiple times, each time against the same INC record, with a margin of error of about 6ms. Occasionally the GlideRecord script even outperformed the GlideAjax script. The outliers happened when the wait time became 4-5 time longer than normal at random intervals.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 09:13 AM
The comment on line 4 of the Script Include was a leftover from copy/paste. Please ignore. Admins - please remove it!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 09:15 AM
I agree Trey. But it would depend on what you are trying to achieve. If its about running some complex queries and getting the data, then you would prefer Ajax call.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2014 09:42 AM
I've been using GlideAjax because it was pushed as being the best practice. If all things are performing perfectly as expected, then I would agree the time difference between GlideAjax and GlideRecord would be negligible. However, if there is a delay of some kind somewhere, the benefits of using GlideAjax really shine because your form does not lock-up because you are waiting for a response. You users can continue on if you are using GlideAjax, but would have to wait if you were using GlideRecord.
Now that being said, client-side GlideRecords can run asynchronously now, which is the method you used in your test case. Just like the g_form.getReference() method can use a callback function, the client-side GlideRecord query() method supports a callback function as well now. Not sure since when, but that was a surprise to me and I just found that out after a post from earlier this week (GlideRecord or GlideAjax?).
So, you can have the best of both worlds now - the simplicity of GlideRecords on the client-side while taking advantage of asynchronous Ajax. I just wanted to point out that if anyone is going to use GlideRecord, make sure to use the callback functionality, like you did in your test case.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2014 03:46 PM
What if we have to use multiple GlideRecord (for example groups, CI, locations, companies...)? (true question, never tried to compare but my guess is the GlideAjax will be better than the Async GlideRecord)
As well, what about the loading time on the form (as the client script has to be loaded)? (again, the difference shouldn't be that important for a small code but for a bigger script, I would put a coin on the GlideAjax)
But from my point of view, the first advantage by using GlideAjax is to explain to new admins / developers that "client side" != "server side", so it can help to avoid mistakes on performance. Sometimes "easy rules" are better than "exact rules"