--- tags: General, Byond, Performance --- # Byond Performance Various notes and anecdotes on byond performance. Please attach a date and/or version number for anything added, as well as note if you have concrete evidence of this or merely suspect it. ## Associative vs. Normal Lists 2021/9/26 Associative lists are a tradeoff between decreased writing speed but increased reading speed (also increased memory cost per item). You need to decide which one to use depending on what you're doing. if you have a big list and you need to be able to quickly find a single element inside it, use an associative list. If all you're doing with the list is iterating through it then there's no point in using an associative list, just use a normal one. ## Performance of `+=` vs `|=` on lists 2021/9/26 With very large lists (eg more than 1000 elements or so), `|=` (unique add/set union) performs drastically slower than `+=` (add), but for smaller lists its about as fast. If high performance lists of unique elements are needed, consider using an associated list. Associated lists are faster to find a single element in for all list sizes and that costs scales very very little with the size of the list unlike normal lists. ``` var/list/example_list = list() //very much speed yes. example_list["unique entry"] = TRUE if(example_list["unique entry"]) //do stuff ``` ## Performance of untyped vs typed loops 2020/4/5 If you are working through a list where you know the list will only contain a specific type, (e.g. the materials list), you should use ``as anything``, which skips the check for the type being passed (e.g ``/datum/material``). ``` for(var/datum/material/material_iterator as anything in SSmaterials.materials) ``` If however you can't be sure what the contents of the list are and only want to iterate through specific types you can do it with a normal typed loop. For example if you only want the items in the contents of a turf. ``` var/turf/current_turf = get_turf(src) for(var/obj/item/item_iterator in current_turf.contents) ``` The difference between these two loop types is that typed loops will run an internal istype check which is not required when iterating through lists where the type is known.