Fixed vs. Dynamic Sized Data Collections in Go 1.18
This time we look at the performance differences between fixed and dynamic size data collections

In Golang, you can use fixed-size datasets like arrays as well as dynamic-size datasets like slices. But have you ever wondered how the performance of your application changes when you choose one over the other? Today, we’ll explore both variants and find out which is faster and how big the difference is. As a small sneak peek, I can already tell you that the result will surprise you. So, I’ll stop talking now, and let’s dive in!!
Intro
Before we begin, let’s look at the different data collections that exist. In the case of fixed-size datasets, there are arrays. On the other hand, we have maps and slices. Slices, in simplified terms, are like arrays with a more general and powerful interface and without a fixed size. A map is what’s called a dictionary or hash in other languages. Simply a data collection where each record has its own custom key.
Now after we have delineated our various data collections, we can formulate our main questions.
- Slices vs. Arrays! Which performs better?
- Slices vs. Maps! Does using a custom key affect performance?
Slices vs Arrays
To compare slices with arrays more deeply, I created benchmark tests for both cases. We start by running them for both approaches. Save the results to a file and finally compare them with benchstat. Below you can see both benchmark tests:
Array Benchmark Test
Slice Benchmark Test
I think both code snippets are largely self-explanatory. You can see two functions for each snippet. In the first, I append many numbers to my array/slice depending on the number I put into the function. The second is my actual benchmark test. Here I call the first function and test how long it takes to enrich our array/slice with all the numbers.
Comparison
Next, we want to know which one is more performant and how big is the difference. To do this, we store the results of both benchmarks and compare them with benchstat.
benchstat slice.txt array.txt
The result is intriguing. I already thought that the approach where we use arrays should be faster than the version with slices. But I wasn’t aware of such a performance gap between both versions. The version with arrays is way faster and the difference increases rapidly with the size of our dataset.

Takeaway
I think most people were already aware that arrays are faster than slices. But such an effect as we saw earlier should sensitize us to pay more attention to the choice of the type to use. The larger our dataset, the more impact our decision will have.
Final Thoughts
This was the first part of “Fixed vs. Dynamic Sized Data Collections.” I hope the comparison of arrays with slices was interesting and you learned something new. If you have anything to mention or questions, it would be great if you leave them in the comments. See you soon.
P.S. This article is part of a new series. Over the next few weeks, I’ll look at various generic helper functions, interesting benchmarks, and useful features.
If you’re as excited as I am, stay tuned!