0

I am using Drupal 8 so there is very little documentation or benchmarks, but I believe a route I have set up for ajax responses is not as performant as it could be. This is the route:

next_node.ajax:
  path: '/node/{node}'
  defaults:
    _controller: '\Drupal\next_node\Controller\DefaultController::view'
  requirements:
    _permission: 'access content'
    _format: 'json'
    _access: 'TRUE'

And this is the controller:

class DefaultController extends EntityViewController {
  /**
   * {@inheritdoc}
   */
  public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
    $build = parent::view($node);

    return new AjaxResponse(\Drupal::service('renderer')->renderPlain($build));
  }
}

I request the content through this function:

jQuery.get( "/node/2?_format=json", function( data ) { /* Do stuff */ });

Is it normal that it takes about a second on average to get a response? Where to optimize the code?

4
  • Maybe you have configured services for development or disabled caching?
    – user21641
    Jul 20, 2015 at 13:34
  • Nope, this is the behaviour with a default install and caching on.
    – alexej_d
    Jul 20, 2015 at 14:51
  • Then you should do some profiling. Using simple Timer should be sufficient for something quick.
    – user21641
    Jul 20, 2015 at 18:37
  • BTW I am using JsonResponse instead of AjaxResponse where I am passing render array and it takes half a second on local to get Search API results and return rendered array with data from multiple entities and 3 search api queries.
    – user21641
    Jul 21, 2015 at 9:55

1 Answer 1

0

There are some different things which could be improved in your code, like using dependency injection, better naming etc, but none of these effect performance.

There are some different things which makes it quite trigger to give a good answer to this. Drupal 8 is still in beta and many performance optimizations are being made. The render cache and page cache should be usable in your case making subsequent requests cached and this a lot faster than the initial request. Being logged in / logged out make a big impact as Drupal has a hard time caching for logged in users, but this is nothing new.

That said, it still sounds a bit slow, but hardware, setup etc also have a lot to say.

So long story short, you can't really performance improve your code.

1
  • Hey there, thank you for the answer. Sorry for the sloppy code, this is really just to illustrate my problem. Well the thing is, that the first request is even faster than the following ones (of course caching is on, but I did not try as an anonymous user – good point) which was the real reason I am so irritated… But yeah, you already helped me – thought there might be some general issues I wasn't aware of.
    – alexej_d
    Jul 23, 2015 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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