I've seen quite a bit of discussion about how kint() from the Devel module is difficult to work with because it maxes out PHP memory limits and makes the browser drag. I've heard some thoughts on improving its speed by limiting the number of levels. For example, in settings.php, you can do the following:

require_once DRUPAL_ROOT . '/modules/contrib/devel/kint/kint/Kint.class.php';
Kint::$maxLevels = 3;

Any other ways to improve the speed of kint()?

  • 1
    The performance issue is caused by the fact that D8 is OOP so there is now a shitload of data to display. So I don't see how it can get any better since it's a JS that really render all of that structured data. – user21641 Aug 18 '16 at 9:36

There are two other ways other users have mentioned for setting the max output levels in addition to the settings.php method mentioned in the question, plus a couple other ideas below:

  1. Create a /modules/contrib/devel/kint/kint/config.php file and add / modify the line $_kintSettings['maxLevels'] = 3; (by @mdrummond on Slack)

  2. In a preprocess function, add the following: (by @thpoul on Slack)

    kint_require();
    Kint::$maxLevels = 3;
    
  3. Be sure to never click the + sign, that shows the entire tree. Click the block instead. You can click the right arrow to open the output in a new window for easier viewing (like when the output is in a block). (by @cwightrun on Slack)

  4. Instead of using kint(), create a preprocess function and use a debugger to explore the variables. (by @danny_englander on Slack)

  5. Pass kint() a specific variable instead of making it roll through the whole tree (by @No Sssweat below).

  6. If you just need to know what variables exist, you can just view the keys by doing a {% for key, value in _context %} loop (by @mortendk below)

  • Should also add Casey's answer from here: drupal.stackexchange.com/a/214713/28813 – Eric Steinborn Sep 27 '16 at 14:07
  • "Be sure to never click the + sign, that shows the entire tree" - Thank you! I was clicking the 'plus' sign because, well, its the symbol for expand. Now I know :-) – tanc Oct 26 '16 at 17:02

Kint is not the only library that you can use for debbuging variables and twig templates.

I suggest to use Symfony var-dumper that IMHO is one of the most powerful lib for debuging variables. Symfony var-dumper is integrated with latest version of devel module (see this issue in the devel issue queue Add a basic symfony var-dumper integration)

For use var-dumper with devel

  • download and install devel module
  • install var-dumper via composer (composer require symfony/var-dumper)
  • go to devel settings page and set var-dumper as the default dumper
  • use {{ devel_dump() }} or {{ kpr() }} or {{ dpm() }} instead of {{ kint() }} for debug variables in twig templates
  • Yes!, It's working! You made my day, thank you! – dotoree Dec 6 '17 at 16:17

Here is another tip:

Instead of using {{ kint() }} which will return everything, you can be more specific by passing the variable that you need; thus, it will load faster.

For example: In node--[type].html.twig you can pass the content variable to kint:

{{ kint(content) }}

If you know the name of the field, you can be even more specific:

{{ kint(content.field_name['#items'].getValue()) }}

This will only return the value(s) of field name.

instead of using kint you can do this little trick instead just to get an idea of whats in there

node.html.twig

<ol>
{% for key, value in _context  %}
<li>{{ key }} </li>
 {% if loop.index == 2 %}}
    <pre>{{ dump( value ) }}</pre>
{% endif %}
{% endfor %}
</ol>

yes it dont gives all the love that kint wants to show you, but at least you get the names etc out

When it is impossible to get the HTML view or it is too slow to load the kint output.

I will serialize the variable and save it with

\Drupal::logger('custom debug')->notice($string);

Then I will copy it to unserialize.com and choose Krumo display....

  • I just want Krumo for D8 – Felix Eve Nov 7 '17 at 23:16

Adding $GLOBALS['_kint_settings']['maxLevels'] = 4; in your settings.local.php should do the trick. Feel free to change the number of levels here.

  • 2
    This does not work for me in Drupal 8.2, because $GLOBALS['_kint_settings']['maxLevels'] in settings.local.php is overwritten by the default value in /modules/contrib/devel/kint/kint/config.php. – Philipp Michael Mar 17 '17 at 8:05

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.