Am I reading this correctly? The standard library has no built in function to get a slice of keys from a map? You need to iterate every single time and build up an extra list??

Well i don't get it.

Get a list of keys from a map to iterate the keys-list instead of the map, doubles the mem and doubles the work.. I never had the case to need the list of keys of a map 🤔 without do something in a iterative way with it.

Do you have an example for this case ?

If this is a java-thing, then better use java 😆

@martinsstar in this case I am passing in an object with a dynamic set of fields to use in a query and I just need the set of keys to compare against other sets within database indexes.

is it faster to do a map loop each time instead of having this slice? I assumed it's more overhead to iterate the entire map instead of an array of keys. Honestly I just want a high level Set type to do unions and diffs.

@mauve
Well, i think do an range on maps will take the same time as range on slices.
Depending on the compare algo, maps can be faster if first look if the key exist ( seeking in a map is a way faster than seeking in a slice, see summary here: blog.devgenius.io/slice-and-ma )

Follow

@martinsstar good to know. I'll try some benchmarking and profiling. :)

@mauve cool would be nice to know if my guess is right.

If two maps need to get compared, i would iterate the first map and look in the second if the key exist. So i guess using of maps will be faster ( because of the key lookup), than to create two arrays of keys and do the lookup there.

But i prefer to use a readable solution over an fast one 😉

Currently i have no time ( maybe tomorrow ) to do an benchmark about this. If you are faster, please inform us about your result 😎 👍

@mauve okay, programmers can not stop thinking about problems...

i do some benchmark of string-lookups in arrays or maps in #golang.

I take the worst case where the first string is not found in the second map/array ( this should only be the case if both objects are completely different )

maps: 41455 ns/op
arrays: 2283822 ns/op

But "iterate" over maps seems a little bit slower ( for 1000000000 ops )

range over maps: 0.0000098 ns/op
range over arrays: 0.0000005 ns/op

paste.bingner.com/paste/esbkb

Sign in to participate in the conversation
Mauvestodon

Escape ship from centralized social media run by Mauve.