{"id":16655553,"url":"https://github.com/aaronrenner/constantizer","last_synced_at":"2025-09-09T06:34:56.381Z","repository":{"id":57485580,"uuid":"134926690","full_name":"aaronrenner/constantizer","owner":"aaronrenner","description":"Helpers to evaluate elixir functions at compile time instead of runtime","archived":false,"fork":false,"pushed_at":"2018-09-25T15:16:31.000Z","size":26,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T15:05:11.526Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aaronrenner.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-26T03:42:18.000Z","updated_at":"2023-06-28T10:16:10.000Z","dependencies_parsed_at":"2022-09-11T16:01:12.684Z","dependency_job_id":null,"html_url":"https://github.com/aaronrenner/constantizer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronrenner%2Fconstantizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronrenner%2Fconstantizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronrenner%2Fconstantizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronrenner%2Fconstantizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aaronrenner","download_url":"https://codeload.github.com/aaronrenner/constantizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221817043,"owners_count":16885452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T09:53:23.160Z","updated_at":"2024-10-28T10:29:23.669Z","avatar_url":"https://github.com/aaronrenner.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Constantizer\n\nTurn functions into constants at compile-time. Very useful when following the\npatterns set forth in the ElixirConf [Growing Applications and Taming\nComplexity][0] talk.\n\n## Installation\n\nJust add `constantizer` to your list of dependencies.\n\n```elixir\ndef deps do\n  [{:constantizer, \"~\u003e 0.2.0\"}]\nend\n```\n\n## Usage\n\nConstantizer allows you to evaluate a 0-arity function at compile time, instead\nof repeatedly evaluating it at runtime. This is very helpful when defining\na public api module with swappable backends.\n\n```elixir\ndefmodule MyApp do\n  import Constantizer\n\n  def register_user(params) do\n    backend().register_user(params)\n  end\n\n  defconstp backend do\n    Application.get_env(:my_app, :backend)\n  end\nend\n```\n\nBy default, `defconst` and `defconstp` will define a public or private function,\nrespectively, with its return value being set to the result of the block as it\nexists at compile time.\n\nTo allow a constant to be evaluated at runtime (for example, so you can inject a\nmock backend in your test environment), modify the following setting in your\nconfig.\n\n```elixir\n# config/test.exs\n\nconfig :constantizer, resolve_at_compile_time: false\n```\n\n## Benchmarks\n\nYou can run constantizer's benchmarks with the following command.\n\n```\nmix run benchmarks/general.exs\n```\n\nHere is some sample output from our benchmarks that call the following module\n\n```elixir\ndefmodule MyModule do\n  import Constantizer\n\n  def runtime_lookup do\n    Application.get_env(:constantizer, :foo)\n  end\n\n  defconst compile_time_lookup do\n    Application.get_env(:constantizer, :foo)\n  end\nend\n```\n\n```\nName                          ips        average  deviation         median         99th %\ncompile_time_lookup       87.43 M      0.0114 μs    ±17.43%      0.0110 μs      0.0130 μs\nruntime_lookup             4.62 M        0.22 μs    ±41.15%        0.21 μs        0.40 μs\n\nComparison:\ncompile_time_lookup       87.43 M\nruntime_lookup             4.62 M - 18.93x slower\n```\n\nThe compile time lookup using `defconst` is 18x faster than using a normal `def` because `Application.get_env/2` is evaluated at compile time instead of runtime.\n\n## Docs\n\nThe docs for this project are available on [hexdocs][1].\n\n[0]: https://www.youtube.com/watch?v=Ue--hvFzr0o\n[1]: https://hexdocs.pm/constantizer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaronrenner%2Fconstantizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faaronrenner%2Fconstantizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaronrenner%2Fconstantizer/lists"}