Skip to content

Adding Alba #1

@vizvamitra

Description

@vizvamitra

Hi,

There is another serialization library named alba, also very performant. On my laptop, it outperforms everyone in attributes/has_one benchmarks and takes 2nd place in has_many (YJIT enabled):

Attributes

Gem IPS Comparison Allocations Comparison
alba 3.6.0 418.366k ± 5.0% baseline 968.000 baseline
panko_serializer 0.8.3 325.561k ±11.6% 1.29x slower 1.016k 1.05x more
transmutation 0.5.1 291.357k ± 5.1% 1.44x slower 1.416k 1.46x more
active_model_serializers 0.10.15 167.788k ± 4.5% 2.49x slower 2.368k 2.45x more
jsonapi-serializer 2.2.0 156.405k ± 7.5% 2.67x slower 3.400k 3.51x more
representable 3.2.0 118.305k ± 5.2% 3.54x slower 4.304k 4.45x more
rabl 0.17.0 88.614k ± 2.2% 4.72x slower 5.856k 6.05x more
jbuilder 2.13.0 70.323k ±10.8% 5.95x slower 2.960k 3.06x more

Has one / Belongs to association

Gem IPS Comparison Allocations Comparison
alba 3.6.0 173.612k ± 2.2% baseline 1.792k baseline
transmutation 0.5.1 123.495k ± 2.1% 1.41x slower 2.832k 1.58x more
panko_serializer 0.8.3 106.747k ± 3.3% 1.63x slower 4.128k 2.30x more
representable 3.2.0 60.661k ± 1.3% 2.86x slower 6.184k 3.45x more
active_model_serializers 0.10.15 50.054k ± 0.9% 3.47x slower 6.384k 3.56x more
rabl 0.17.0 46.370k ± 3.4% 3.74x slower 9.608k 5.36x more
jsonapi-serializer 2.2.0 40.628k ± 1.4% 4.27x slower 12.368k 6.90x more
jbuilder 2.13.0 34.031k ± 1.6% 5.10x slower 4.944k 2.76x more

Has many association

Gem IPS Comparison Allocations Comparison
panko_serializer 0.8.3 206.260k ± 5.0% baseline 1.376k baseline
alba 3.6.0 130.061k ± 1.2% 1.59x slower 2.320k 1.69x more
transmutation 0.5.1 81.483k ± 1.8% 2.53x slower 4.408k 3.20x more
representable 3.2.0 39.688k ± 1.8% 5.20x slower 9.640k 7.01x more
active_model_serializers 0.10.15 34.400k ± 3.7% 6.00x slower 9.480k 6.89x more
jbuilder 2.13.0 32.044k ± 5.9% 6.44x slower 6.208k 4.51x more
jsonapi-serializer 2.2.0 30.699k ± 0.7% 6.72x slower 16.408k 11.92x more
rabl 0.17.0 22.172k ± 1.4% 9.30x slower 10.912k 7.93x more

Thought it would be worth adding it to the benchmarks. Sorry for providing the code instead of making a PR: I though it'd be wrong to use benchmark results from another laptop in the Readme.md

# Gemfile
#
gem "alba"

# lib/serializers/alba/organisation_serializer.rb
#
module Alba
  class OrganisationSerializer
    include Alba::Resource

    attributes :id, :name

    attribute :logo_url do
      "https://example.com/logos/companies/#{object.id}"
    end
  end
end

# lib/serializers/alba/post_serializer.rb
#
module Alba
  class PostSerializer
    include Alba::Resource

    attributes :id, :title, :body

    one :user, resource: Alba::UserSerializer
  end
end

# lib/serializers/alba/user_serializer.rb
#
module Alba
  class UserSerializer
    include Alba::Resource

    attributes :id, :first_name

    attribute :full_name do
      "#{object.first_name} #{object.last_name}"
    end

    many :posts, resource: Alba::PostSerializer
  end
end

# benchmark.rb
#
GemBenchmarks.report output: true do
  group("Attributes") do
    # ...
    example("alba")                     { Alba::OrganisationSerializer.new(organisation).serialize }
  end

  group("Has One / Belongs To") do
    # ...
    example("alba")                     { Alba::PostSerializer.new(post, within: [:user]).serialize }
  end

  group("Has Many") do
    # ...
    example("alba")                     { Alba::UserSerializer.new(user, within: [:posts]).serialize }
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions