A little out of the scope, but I see frequently in js code something like this:
const array = new Array(1e5).fill(void 0).map((v, i) => { return { prop: 'test-' + i }; } );
The purpose is to fill the array with variable values, instead of fill() which only allow one value. The above code results in really non optimized initialization of array :
The array is filled with non useful values (here void 0), else map or forEach won't work.
The array is duplicated because of map which returns a different array.
We could have done:
const array = new Array(1e5);
for (let i = 0, l = array.length; i < l; i++) {
array[i] = { prop: 'test-' + i };
}
But this last one is far more verbose.
It would be great to have something like array.fillMap(callback, [start, [end]]), where callback is the same as map or forEach but put data inside of array instead. The return will be the array itself. This could allow some important initialization optimization or data filling from the js engine.
The above code could be rewrite as:
const array = new Array(1e5).fillMap((v, i) => { return { prop: 'test-' + i }; } );
Finally, the fillMap is just an idea, others names could be used.
A little out of the scope, but I see frequently in js code something like this:
The purpose is to fill the array with variable values, instead of
fill()
which only allow one value. The above code results in really non optimized initialization of array :void 0
), elsemap
orforEach
won't work.map
which returns a different array.We could have done:
But this last one is far more verbose.
It would be great to have something like
array.fillMap(callback, [start, [end]])
, wherecallback
is the same asmap
orforEach
but put data inside of array instead. The return will be the array itself. This could allow some important initialization optimization or data filling from the js engine.The above code could be rewrite as:
Finally, the
fillMap
is just an idea, others names could be used.Let me know what you think !