props_template 1.0: A high performance, more opinionated jbuilder replacement

We released props_template over a year ago and it’s exciting to see it being adopted by folks. For us, its been the workhorse behind Superglue and powers its most dyanamic features. Today we’re finally releasing 1.0.

Fast

props_template is among the fastest JSON builders available today. The Props::Base class that powers props_template can also be used standalone.

    props_base_class:     1439.9 i/s
               panko:     1287.6 i/s - 1.12x  slower
      props_template:      998.8 i/s - 1.44x  slower
       turbostreamer:      912.9 i/s - 1.58x  slower
                alba:      871.0 i/s - 1.65x  slower
         jserializer:      668.7 i/s - 2.15x  slower
alba_with_transformation:      604.2 i/s - 2.38x  slower
              barley:      452.2 i/s - 3.18x  slower
        barley_cache:      441.4 i/s - 3.26x  slower
            jbuilder:      432.6 i/s - 3.33x  slower
     fast_serializer:      390.1 i/s - 3.69x  slower
               rails:      374.1 i/s - 3.85x  slower
                rabl:      310.3 i/s - 4.64x  slower
         blueprinter:      268.4 i/s - 5.36x  slower
       representable:      187.2 i/s - 7.69x  slower
          simple_ams:      124.5 i/s - 11.57x  slower
                 ams:       41.5 i/s - 34.67x  slower
         alba_inline:       10.9 i/s - 131.64x  slower

No auto format

Sometimes omitting features is a feature in itself. props_template DOES NOT have a feature to auto format your keys. If you need it to be camelCase, we encourage you do it manually.

json.firstName "John Smith"

vs. Jbuilder

json.key_format! camelize: :lower
json.first_name "John Smith"

There’s really good reason why we do this: It’s easily greppable. If you’re debugging a frontend app and trying to figure out where firstName came from, a quick search for firstName among .rb files will help you get to the right starting point. And the side effect of avoiding camelize is a nice performance boost as well.

Improved reading experience

We strive to make the payload shape more obvious by designing features around key alignment. Its a small improvement that pays dividends as your JSON payload gets larger and more complex.

json.person(cache: ['v1', @person]) do
  json.name @person.name
  json.age @person.age
end

vs. Jbuilder

json.person do
  json.cache! ['v1', @person] do
    json.name @person.name
    json.age @person.age
  end
end

Superglue Powers

props_template has features that power Superglue, The Rails way of working with React and Rails, but you can use them in interesting ways even if you don’t use Superglue. Here’s an example using digging:

json.data do
  json.header do
    sleep 5
    json.greeting "hello"
  end

  json.content do
    json.body "World"
  end
end

The above would wait 5 seconds before returning:

{
  data: {
    header: {greeting: "hello},
    content: {body: "World"}
  }
}

With digging:

json.data(dig: ['data', 'content']) do
  json.header do
    sleep 5
    json.greeting "hello"
  end

  json.content do
    json.body "World"
  end
end

Would run immediately and return

{
  data: {
    body: "World"
  }
}

Its a builtin performance optimization for scenarios where you have to “pluck” a part of the payload without loading others.

Give it a try!

About thoughtbot

We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development. Learn more about us.