lucacorti / jsonapi_plug

JSON:API library for Plug and Phoenix applications
https://hexdocs.pm/jsonapi_plug
MIT License
11 stars 3 forks source link

Precompute casing #54

Closed treere closed 6 months ago

treere commented 6 months ago

The field casing is computer at runtime. The fields are always the same so I precomputed them at compile time

To test:

  test "bench" do
    conn =
      conn(:get, "/posts?include=author")
      |> Conn.assign(:data, [@default_data])
      |> Conn.assign(:meta, %{total_pages: 1})

    Benchee.run(%{"call" => fn -> MyPostPlug.call(conn, []) end}, time: 10, memory_time: 2)
  end

Before:

Name           ips        average  deviation         median         99th %
call        8.13 K      123.06 μs    ±15.65%      115.53 μs      181.54 μs

Memory usage statistics:

Name    Memory usage
call        51.73 KB

After

Name           ips        average  deviation         median         99th %
call       12.55 K       79.71 μs    ±30.91%       69.56 μs      179.22 μs

Memory usage statistics:

Name    Memory usage
call        35.93 KB
lucacorti commented 6 months ago

I think you can also change the fields, include and sort query parsers to call resource.recase_field/2 instead of JSONAPIPlug.recase/2.