J3 Issue ?
avatar mbabker
mbabker
23 Oct 2018

Calls to Joomla\CMS\HTML\HTMLHelper::date() (or Joomla\CMS\HTML\HTMLHelper::_('date') if you're Doing It Right) can be somewhat expensive because it always calls Joomla\CMS\Factory::getUser() which causes a read action from the session API. This method is used quite liberally to format date/time strings to a desired timezone, so it should be a performant method to the best of our ability. Additionally, it's quite a bit of overhead if you don't need to convert timezones on a date/time string. So, there are a couple of actionable points with this issue:

First, there is only one code path that requires a call to Joomla\CMS\Factory::getUser() so we shouldn't make that call every time the method is executed.

Second, we should review the core uses of this method and depending on the use case decide if:

  • We can specify a timezone for use or avoid use of the method at all (versus localizing the date/time string)
  • We actually need a translatable date/time string or a standardized format can be used

Right now, this code snippet is massively more performant (and performance matters a lot in places like the action logs exports, as many recent issues have shown, not to mention the method is normally used in all layouts where date/time strings are displayed to users, so it gets used quite frequently):

use Joomla\CMS\Date\Date;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

// This ...
$date = new Date($time, new \DateTimeZone('UTC'));
$date->format(Text::_('DATE_FORMAT_LC6'));

// ... is roughly equivalent to ...
HTMLHelper::_('date', $time, Text::_('DATE_FORMAT_LC6'), 'UTC');

// ... but without the performance overhead.
avatar mbabker mbabker - open - 23 Oct 2018
avatar joomla-cms-bot joomla-cms-bot - change - 23 Oct 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 23 Oct 2018
avatar laoneo
laoneo - comment - 24 Oct 2018

This code snippet formats the date into UTC. But mostly you want to have the date in the user timezone on display in a page. So there is no way around to get the timezone of the user somehow.

avatar mbabker
mbabker - comment - 24 Oct 2018

When you're aiming for user timezone conversion, then yes, the JHtml helper should be used. My main point though is that not every date/time display in Joomla actually needs to go through this process, and that is what we can/should review. Though with other patches the performance penalty is getting negated, it still doesn't hurt to do a scan and make sure we aren't needlessly converting.

avatar mbabker mbabker - change - 24 Oct 2018
The description was changed
avatar mbabker mbabker - edited - 24 Oct 2018
avatar mbabker mbabker - edited - 24 Oct 2018
avatar mbabker mbabker - change - 24 Oct 2018
The description was changed
avatar brianteeman brianteeman - change - 30 Oct 2018
Labels Added: J3 Issue
avatar brianteeman brianteeman - labeled - 30 Oct 2018
avatar mbabker mbabker - change - 19 Feb 2019
Status New Closed
Closed_Date 0000-00-00 00:00:00 2019-02-19 02:01:01
Closed_By mbabker
avatar mbabker mbabker - close - 19 Feb 2019

Add a Comment

Login with GitHub to post a comment