|
|
|
|
|
by scottlamb
2591 days ago
|
|
Typically (I don't know about in Javascript in particular), a vector has both a length and a capacity. The capacity represents total allocated space (always >= the length). If excess capacity is available when you append, that gets used rather than having to copy the array. The capacity grows exponentially (powers of 2 or 1.5 or something). This means that if you take an empty vector and append N elements one at a time, it does O(N) total copying of existing values rather than O(N^2). Another way to put it is that a single append is amortized constant time. Here's a stackoverflow answer which talks a bit more about this: https://cs.stackexchange.com/a/9382 |
|