Is @Produces more efficient that a getter in an EL expression
Asked Answered
S

1

0

I'm wondering which of those two code snippets is more efficient.


First one

In userSearch.xhtml :

<rich:dataTable
    var="user"
    value="#{userSearchResultList}"
    rendered="#{not empty userSearchResultList}">
...
</rich:dataTable>

In UserSearchAction.java :

@Produces @RequestScoped
@Named("userSearchResultList")
public List<User> getResultList() {
    return resultList;
}

Second one

In userSearch.xhtml :

<rich:dataTable
    var="user"
    value="#{userSearchAction.resultList}"
    rendered="#{not empty userSearchAction.resultList}">
...
</rich:dataTable>

In UserSearchAction.java :

public List<User> getResultList() {
    return resultList;
}

In both solutions, my resultList variable is filled by a method UserSearchAction.search().

I'm using JBoss 7.0.2.Final and RichFaces 4.1.0.Final.

More generally, I wanted to know if it's better to write producers than to call sub-properties of some classes in JSF files.

Sauterne answered 21/12, 2011 at 15:53 Comment(0)
U
1

That depends on how your producer scopes what is being produced. If it's dependent scoped (meaning you don't have a scope on it, nor on the containing class) it ends up being the same, possibly less depending what it is you are having to do inside that method.

In your example it should be more efficient because that producer method should only be called once (per request).

Ulmer answered 23/12, 2011 at 7:5 Comment(2)
Yes, it was more or less what I was thinking... but I wanted to know if the code used in JBoss finally makes one solution better than the other... but I think that I couldn't know for real until I make stress test for that... thanks anyway.Sauterne
Good point about being called only once! JSF can resolve a property many times, in extreme cases tens of times per request. The producer is essentially a kind of declarative alternative to caching data in an instance variable. It might be even more efficient to use a "cheaper" scope than request scope. The downside I think is readability. With the second approach it's more clear where the data comes from.Richie

© 2022 - 2024 — McMap. All rights reserved.