DEV Community

Discussion on: Using NEW, instead of MAKE, to create slice

Collapse
 
freakynit profile image
Nitin Bansal

Did the benchmarks. Here is the code and the results: gist.github.com/freakynit/69cacf64...

Observation: Eager Dereferenced version using new always beat other two (non-eager-dereferenced and make).

Collapse
 
yusufpapurcu profile image
Yusuf Turhan Papurcu

Did you tested with structs?
I changed statsHolder := make([]Stats, 0, 11664000) to statsHolder := *new([]Stats) but performance diff is 4x. First one works in ~500ms but second one almost take 2 second. I uploaded example benchmark to here.

Thread Thread
 
freakynit profile image
Nitin Bansal

That will be there. Basically the difference is due to pre-allocation of memory vs on-demand allocation and resizing. See my later comment reply: dev.to/freakynit/comment/1k4fk

Thread Thread
 
yusufpapurcu profile image
Yusuf Turhan Papurcu

Oh I understand now. Thanks for answer 👍