Recently I encountered some programming challenges where I had to populate an array with a default value. After researching various approaches, I am unsure about which option would provide the best performance.
As an example, let's say I want to fill an array of size 10 with the value 0. Here are the options I considered:
Option One
let arr = []
arr.length = 10
arr.fill(0)
console.log(arr)
Option Two
let arr = new Array(10).fill(0)
console.log(arr)
Option Three
let arr = Array(10).fill(0)
console.log(arr)
Option Four
function makeNewArray(size, value) {
let arr = []
for (let i=1; i<=size; i++)
arr.push(value)
return arr
}
let arr = makeNewArray(10,0)
console.log(arr)
I am uncertain as to which approach is considered standard and offers faster compilation times. Are there any other superior methods worth considering?