VK
let array = [3, 0, 0, 1, 2, 0, 5, 4, 3, 3];
const noZerosArray = array.filter(item => item);
const zerosArray = noZerosArray.concat(Array(array.length - noZerosArray.length).fill(0))
Size: a a a
VK
let array = [3, 0, 0, 1, 2, 0, 5, 4, 3, 3];
const noZerosArray = array.filter(item => item);
const zerosArray = noZerosArray.concat(Array(array.length - noZerosArray.length).fill(0))
VK
function Zeros(arr) {
let newArr = [];
for (let i = array.length - 1; i >= 0; --i) {
if (arr[i] != 0){
newArr.unshift(arr[i]);
} else {
newArr.push(arr[i]);
}
}
return newArr;
}
VK
NR
p
VK
p
VK
NR
VK
Here we see that the internal array used as the backing store has changed to <FixedArray[17]> [PACKED_SMI_ELEMENTS]. The new array has the same elements kind, but a different address, and the internal array size equal to 17. On our 64-bit system, this means that it takes 17 * 8=136 bytes of memory (for the sake of simplicity, we ignore object headers). It also means that the allocated internal array is bigger than what we requested. This allows V8 to achieve constant amortized time for push() and similar operations that grow the array. The following formula is used to determine the new size in situations when the internal array is not enough:
new_capacity = (old_capacity + 50%) + 16
NR
DE
VK
> [3, 0, 0, 1, 2, 0, 5, 4, 3, 3].sort((a, b) => a && b || (b - a))
[
3, 1, 2, 5, 4,
3, 3, 0, 0, 0
]
В
NR
VK
T
p
p
В