satoru
satoru

Reputation: 33225

How can I specify initial capacity of a growable list in Dart?

In Go, a slice could be created with initial capacity to avoid unnecessary memory allocations and copies.

In Dart, I know I can create a List with fixed size with something like new List(10), but how can I create one that's also growable?

Upvotes: 5

Views: 986

Answers (1)

lrn
lrn

Reputation: 71763

You cannot.

The Dart growable list does not provide any way to control its capacity. It's entirely under the control of the underlying implementation.

Dart does not make any promises about how a growable list is implemented, not even that there is a potentially longer list backing it. Providing a way to control the length of such a list would not make sense if there isn't one.

When Dart is compiled to JavaScript, a Dart growable list is implemented by a JavaScript Array which also does not provide any way to control its initial capacity. That is one of the reasons Dart does not specify its implementation of growable lists - it actually doesn't know because it depends on the JavaScript implementation.

Upvotes: 6

Related Questions