ruby on rails - Using a frozen constant as a hash key? -
i built small api makes few calls have similar payloads, have base payload merge in call-specific elements, so:
def foo_call base_payload.merge({"request.id" => request_id}) end def biz_call base_payload.merge({"request.id" => some_other_thing}) end def base_payload { bar: bar, baz: baz, "request.id" => default_id } end
my coworker suggested make "request.id"
frozen constant, arguing making constant means can freeze means wont allocating new string object on each call, saving bit of memory. this:
requestid = "request.id".freeze def foo_call base_payload.merge({requestid => request_id}) end def biz_call base_payload.merge({requestid => some_other_thing}) end def base_payload { bar: bar, baz: baz, requestid => default_id } end
i'm little apprehensive, can't quite pin down reason why not (other latent resistance @ having new commit :>
). feel might cause weirdness have same object key in multiple hashes -- merging in object "request.id"
might not overwrite original string base_payload -- or won't see memory saved since hash have clone string hash key anyway, i'm not sure.
am being overly paranoid/resistant?
Comments
Post a Comment