Skip to content

Performance: Bypass Ember's normalizeResponse

Mike Pastore edited this page Feb 12, 2017 · 8 revisions

Summary

The default DS.JSONAPISerializer class that comes with Ember Data works great out of the box with Sinja and JSONAPI::Serializers. However, it performs a lot of costly, unnecessary work in order to normalize already-mostly-normalized {json:api} responses. If your Ember application primarily talks to Sinja, and your Sinja application primarily talks to Ember, you may be able to bypass the normalizeResponse hook in your application-wide DS.JSONAPISerializer entirely and greatly speed things up.

HOWTO

Sinja

  1. Write your base serializer like this to singularize types and camelCase attribute and relationship names while preserving the default link generation behavior:

    require 'jsonapi/ember_serializer'
    
    class BaseSerializer
      include JSONAPI::EmberSerializer
    
      # ..
    end

Ember

  1. Generate an application-wide serializer (if one doesn't already exist):

    $ ember generate serializer application
  2. Add this method to the generated serializer:

    normalizeResponse(_store, _primaryModelClass, payload, _id, _requestType) {
      return payload;
    }

    Add /* jshint unused:vars */ to the top of the file to ignore the JSHint warning, or /* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */ to ignore the ESLint warning.

Notes

You can still, of course, customize the JSONAPI::Serializers in your Sinja application and the individual DS.JSONAPISerializer classes in your Ember application if you need to do anything gnarly. This just sets a high-performing default for the "happy path."

Please note that all your {json:api} includes will have to be camelCase going forward to be compatible with this change. If your primary target is Ember, this shouldn't be a problem!

Credit

Hat tip to Chris Thoburn for sharing this awesome performance tip on Ember Weekend!

Clone this wiki locally