Julia building strings with no allocation

Viewed 154

I am trying to build a string up from small parts to send to a websocket.

#Example input from somewhere else
side = "left"
jumps = 3.482
string_of_id = "Caterpillar"
sz = 100
speller = "lower"

function stringConcat(side::String, px::Float64, ticker::String, sz::Int64, ordType::String)

    return string("""{"id":\"""", string(abs(rand(Int64))), """\",\"message\":[{\"Key1\":""", string(rand(1:10000)), """,\"Leftorright\":\"""", side, 
                    """\",\"numjump\":""", string(jumps), """,\"jumpid\":\"""", string_of_id, """\",\"sz\":""", string(sz), """,\"speller\":\"""", type_of_speller,
                    """\",\"mode\":\"json\"}],"operation":"jumping\"}""")
end

# 255.780 ns (9 allocations: 911 bytes)

Is there a way to preallocate to avoid allocations, or other tricks I can use? Right now I believe this many allocations is quite excessive.

1 Answers
Related