@shashi_mulugu @krati_garg
Thanks for your inputs.
Our code creates content fragments and their variations and then populates their element values. If I call .listAllVariations() and get the variationDefinition, I will still need to call getElements as it's not the title/description of the variation I want to update but its element values. Further more, we only really call one commit at the end of the process.
I've re-written the code and added timings and comments:
public static void createVariationNew(ContentFragment cf, JsonObject dp, String variationName, String variationTitle, Logger log) throws ContentFragmentException, PersistenceException, RepositoryException {
long startTime = System.currentTimeMillis();
//create the variation
cf.createVariation(variationName, variationTitle, "");
Resource cfResource = cf.adaptTo(Resource.class);
Resource variationResource = cfResource.getChild(JcrConstants.JCR_CONTENT + "/data/" + variationName);
Node variationNode = variationResource.adaptTo(Node.class);
// now populate each element with the values I want to seed it with.
for (String element: dp.keySet()){
variationNode.setProperty(element, dp.getString(element, ""));
}
log.debug("Time to create NEW {}: {}", variationName.substring(0,variationName.indexOf("-")), (System.currentTimeMillis() - startTime));
}
public static void createVariationOld(ContentFragment cf, JsonObject dp, String variationName, String variationTitle, Logger log) throws ContentFragmentException, PersistenceException {
long startTime = System.currentTimeMillis();
//create the variation
VariationTemplate vt = cf.createVariation(variationName, variationTitle, "");
//get all elements for that variation
Iterator<ContentElement> itr = cf.getElements();
//for each element
while(itr.hasNext()) {
ContentElement cfElement = itr.next();
//grab the variation for that element - but i already have my variation from cf.createVariation
ContentVariation cv = cfElement.getVariation(vt.getName());
//now update the element value for that variation
cv.setContent(dp.getString(cfElement.getName(), ""), "String");
}
log.debug("Time to create OLD {}: {}", variationName.substring(0,variationName.indexOf("-")), (System.currentTimeMillis() - startTime));
}
Toggling between createVariatonOld and createVariationNew gives me the following outcomes:

If I use com.adobe.cq.dam.cfm.ContentFragment.getElements and com.adobe.cq.dam.cfm.ContentVariation.getVariation the time taken doubles compared to using javax.jcr.Node.setProperty. The other time consuming call is com.adobe.cq.dam.cfm.ContentFragment.createVariation. I probably could reduce the time by using javax.jcr.Node to create the required variation nodes but am hesitant as that one call seems to create /model/variations/myVariation node as well as the /data/myVariation.
In the documentation com.adobe.cq.dam.cfm.ContentFragment is the recommended way of creating content fragment variaitons but why is it less efficient that using just javax.jcr.Node.setProperty?