This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Aug 13, 2013, 4:41 PM
4 Posts

Server side code execute twice

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.3
  • Role:
  • Tags:
  • Replies: 10

Hi

I create a very basic xpage, link it to a source document, and create a list box component. In the list box values I set a basic ssjs formula as follows:

print("Getting formula items for list box");
return new Array("Test1","Test2");

My question is, why does it print twice in the console when I load the xpage? Is this part of the jsf cycle? I am just concerned about performance when I have such code in more complex xpages.

Thanks

Ed

Aug 13, 2013, 5:56 PM
586 Posts
Yep

This is absolutely part of the JSF lifecycle.  Sometimes things will get run more then once so you do always want to be wary of performance. Though in all honestly I'm not always and unless you're doing something REALLY silly you might not have any problems even if they do run more then once.  It's really amazing how fast XPages is for all that's going on.

Sadly I barely understand the JSF lifecycle so I can't explain it.  There are some ways to improve performance by skipping over certain phases.  Bruce Elgort did a presentation long ago about it.  I know Paul Withers has talked about it on his blog.  Some of the XPages dev team have released some videos...  I THINK over on the OpenNTF youTube channel that deals with performance and the lifecycle.  Those came from a training seminar that they gave a while back.  Probably no better resource then there.  It's probably also discussed in the Mastering XPages book.

It is an issue and something to put on your radar to learn more about, but in normal it does not cause me to go out of my way when I develop my apps yet.  Honestly I'm just happy my stuff works some days. haha  :)  So I wouldn't lose sleep over it.  I'd suggest build the app first then fine tune as necessary.

Dave

Aug 13, 2013, 7:10 PM
4 Posts
Re: Yep

Hi Dave

 

Thanks for your feedback. 

If I set the option to "compute on page load" rather than "computed dynamically" it will only run once. But that is not always possible because such code won't be executed when a component triggers a full or partial update.

Aug 13, 2013, 9:02 PM
586 Posts
right

Compute on Page load is not the answer for everything.  Again - this might not be a huge deal that things run twice here and there.  At least as you're starting out.  If you're developing the next Facebook then yes it would be a big issue. There are some things like "Partial execution" that are designed to help this.  But look into some of those sources I mentioned.

The JSF lifecycle is important to have on the roadmap of xpages learning.  I just don't think it's the most important thing in the early stages.

Aug 14, 2013, 8:44 AM
17 Posts
Re

I'm not a Pro about the JSF Lifecycle, but i know that things are executed twice.. this is especially annoying when doing e.g. a view lookup for a Repeat Control..

I guess because it's executed on the SET and on the GET.. so when a page is posted, and then when it is rendered again..

One thing to do is doing your own caching, so when you make a view lookup, then store the values in a list.. execute the lookup only, if the list is null.. If you have a dependant list, then clear the cached list, if the value from which the list depends on is changed.

This stuff is much simpler and more transparent if you are using Java (Managed Beans).. I just finished a pretty big XPages Application, without a single line of SSJS (except calls to Managed Beans with parameters.), It resulted in about 40 Java classes (Controller and Object Classes).. If i would have tried that with SSJS, i would be in the mad house right now!!

Aug 17, 2013, 5:28 PM
453 Posts
Here is something that I have done

I have some code that needs to run conditionally where I have a function as part of the condition.

So say I have a rendered property on a panel and if it renders is based on several facts

(viewScope.get("vsSomeVariable") & myFuntion("SomeParameter") : ? true : false;

also myFunction does a lot of stuff including getting a bunch of documents and cycling through them. So using the shorthand form is not an efficeint way to go.

If(viewScope.get("vsSomeVariable")){

return myFunction("SomeParameter");

} else {

return False;

}

So the viewScope.get is a cheap call and if it false then there is no need to call myFunction. This might be obvious to some but I know I sure missed it until I took a harder look at the code due to an error in myFunction and found it was getting called many times in the life cycle of the view that it was part of. I always try to write these formulas in the shorthand, but it does not always produce the best results. Simple is generally best but not always.

Aug 17, 2013, 6:43 PM
298 Posts
Bill, good suggestion but a question?
Isn't there a typo in your if statement test?  Didn't you mean && instead of & for the logical and?

Just to clarify for the other folks who are new to JS.

Howard

viewScope.get("vsSomeVariable") & (<<here) myFuntion("SomeParameter") : ? true : false;

also myFunction does a lot of stuff including getting a bunch of documents and cycling through them. So using the shorthand form is not an efficeint way to go.

If(viewScope.get("vsSomeVariable")){

return myFunction("SomeParameter");

} else {

return False;

}

Aug 18, 2013, 7:48 PM
453 Posts
Interesting use of | and ||

After Howard's comment on the use of || and && in a JS if statement I was looking through some of my code and found several instances where I had just specified a single | or & and the evaluation seems to be working correctly. So it would appear that the || vs | is not strictly enforced. So is the double | and & a best practice or am I missing something?

Aug 19, 2013, 2:01 PM
586 Posts
Doubles....

Bill,

I've always done &&...  I've never done a single...  If it looks like a single works I'd be really careful about that.  I bet it might not hold up in every case.  But I don't know.

 

Aug 19, 2013, 3:21 PM
453 Posts
Kinda like

You can use a single "=" in a comparison. It does not through an error but does the assignment rather than the comparison. Spent hours one day trying to track that down until I found that. Easy fix hard to find.

But then no one would ever do anything that stupid would they :-)


This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal