关于vuejs /vue-next 中使用 for 填充数组的讨论
fill for new Array(len) 初始化赋值
packages/runtime-core/src/createRenderer.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 const newIndexToOldIndexMap = []for (i = 0 ; i < toBePatched; i++) newIndexToOldIndexMap.push(0 )const newIndexToOldIndexMap = new Array (toBePatched).fill(0 )const newIndexToOldIndexMap = new Array (toBePatched)for (i = 0 ; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
讨论地址 https://github.com/vuejs/vue-next/pull/319
执行测试结果 https://jsperf.com/array-fill-vs-loop
new Array(toBePatched) 数组可开辟固定大小, 不需计算扩容
Array.prototype.fill() 基础 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
1 2 3 4 5 6 7 8 9 10 11 12 fill(value, start, end) arr.fill(value[, start[, end]]) value Value to fill the array with . (Note all elements in the array will be this exact value.) start Optional Start index (inclusive), default 0. end Optional End index (exclusive), default arr.length.
如有问题可联系 Email:afacode@outlook.com 或 微信:afacode