module TaxUtilv2
Overview
This module contains the code for calculating taxes. Author: Maheep Kumar (technusm1)
Defined in:
tax_util_v2.crConstant Summary
-
BRACKETS_NEW =
{percent: {0.10, 0.12, 0.22, 0.24, 0.32, 0.35, 0.37}, single: {0, 9875, 40125, 85525, 163300, 207350, 518400}, married: {0, 19750, 80250, 171050, 326600, 414700, 622050}, hoh: {0, 14100, 53700, 85500, 163300, 207350, 518400}}
-
BRACKETS_NEW uses tuples instead of arrays and namedtuple instead of hash Update find_index_in_* functions if BRACKETS_NEW is updated
-
PRECOMPUTED_TAXES =
{single: BRACKETS_NEW[:single].map do |income| calc_taxes(income, :single) end, married: BRACKETS_NEW[:married].map do |income| calc_taxes(income, :married) end, hoh: BRACKETS_NEW[:hoh].map do |income| calc_taxes(income, :hoh) end}
-
We are precomputing the taxes for each bracket limits
Class Method Summary
-
.calc_taxes(income, status = :single)
Initial implementation (faster than original code): - using symbols instead of strings - using a simple loop instead of zip processing multiple arrays
-
.calc_taxes_precompute(income, status = :single)
Implementation with precomputed taxes.
-
.calc_taxes_precompute_with_precompute_indices(income, status = :single)
Implementation with precomputed taxes and precomputed indices.
Class Method Detail
Initial implementation (faster than original code):
- using symbols instead of strings
- using a simple loop instead of zip processing multiple arrays
Implementation with precomputed taxes. Works better than our initial implementation, since we are using PRECOMPUTED_TAXES to improve the performance of tax calculation step
NOTE I tried using binary search to find the index of the bracket, but the performance was slower than the current implementation. Could be that the bracket is too small for binary search to be effective.
Implementation with precomputed taxes and precomputed indices. Works better than our initial implementation, and the precompute implementation above Since we are using precomputed indices, we can get rid of the rindex call completely The cost comes in terms of code maintainability, since we need to update the find_index_in_* functions if the brackets are updated. This is a tradeoff we are making for performance.