Skip to content

added faster transformation to snake case #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## CHANGELOG

v0.10.0:
- added high performance custom transformation to snake case (underscore), which is more than 2 times faster than the original

v0.9.0:
- underscore replaces now hyphens (https://github.com/ruby2elixir/atomic_map/pull/5 + https://github.com/ruby2elixir/atomic_map/pull/6)
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: fa
iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: false )
%{CamelCase: [%{c: 1}, %{c: 2}]}

# in case you have extra large maps and you wish to use the faster custom transformation to underscore, you can pass `high_perf: true`
AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, high_perf: true)
%{camel_case: [%{c: 1}, %{c: 2}]}

# hyphens are replaced
iex> AtomicMap.convert(%{ "some-key" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: true )
%{some_key: [%{c: 1}, %{c: 2}]}
Expand All @@ -41,7 +45,7 @@ AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: true, i
1. Add atomic_map to your list of dependencies in `mix.exs`:

def deps do
[{:atomic_map, "~> 0.8"}]
[{:atomic_map, "~> 0.10"}]
end

## Todo:
Expand Down
22 changes: 22 additions & 0 deletions bench/basic_bench.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ defmodule BasicBench do
@long_string "StringShouldChange-some_stuffStringShouldChange-some_stuffStringShouldChange-some_stuffStringShouldChange-some_stuffStringShouldChange-some_stuff"
@short_string "StringShouldChange-some_stuff"

@payload_camel_cased File.read!("bench/test-payload-camel-cased.json") |> Poison.decode!()
@large_payload_camel_cased 1..100 |> Enum.reduce(%{}, fn(x, acc) -> Map.put(acc, "key#{x}", @payload_camel_cased) end)

@payload_snake_cased File.read!("bench/test-payload-camel-cased.json") |> Poison.decode!()
@large_payload_snake_cased 1..100 |> Enum.reduce(%{}, fn(x, acc) -> Map.put(acc, "key#{x}", @payload_snake_cased) end)

bench "macro_underscore - long" do
@long_string |> macro_underscore
end
Expand All @@ -33,4 +39,20 @@ defmodule BasicBench do
bench "regex_underscore - short" do
@short_string |> regex_underscore
end

bench "large dict camel cased with low perf underscore" do
@large_payload_camel_cased |> AtomicMap.convert(safe: false, underscore: true, high_perf: false)
end

bench "large dict camel cased with high perf underscore" do
@large_payload_camel_cased |> AtomicMap.convert(safe: false, underscore: true, high_perf: true)
end

bench "large dict snake cased with low perf underscore" do
@large_payload_snake_cased |> AtomicMap.convert(safe: false, underscore: true, high_perf: false)
end

bench "large dict snake cased with high perf underscore" do
@large_payload_snake_cased |> AtomicMap.convert(safe: false, underscore: true, high_perf: true)
end
end
6 changes: 0 additions & 6 deletions bench/snapshots/2016-05-04_22-21-57.snapshot

This file was deleted.

10 changes: 10 additions & 0 deletions bench/snapshots/2017-11-21_11-13-42.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
duration:1.0;mem stats:false;sys mem stats:false
module;test;tags;iterations;elapsed
BasicBench large dict camel cased with high perf underscore 10 1256231
BasicBench large dict camel cased with low perf underscore 5 1467331
BasicBench large dict snake cased with high perf underscore 10 1223200
BasicBench large dict snake cased with low perf underscore 5 1507264
BasicBench macro_underscore - long 50000 2172177
BasicBench macro_underscore - short 100000 1185552
BasicBench regex_underscore - long 50000 2968897
BasicBench regex_underscore - short 100000 1680492
Loading