4 ms·
You could do it like this: const mapNodeList = (...nodeList) => nodeList.map(myFn); mapNodeList(...$nodeList)
by nicoburns 5y ago
You could do it like this:
const mapNodeList = (...nodeList) => nodeList.map(myFn);
mapNodeList(...$nodeList)
- hajile 5y agoThat creates an entire extra list of garbage.
- chrisseaton 5y agoDoesn’t the compiler optimise it away?
- hajile 5y agoThose aren't the same types. One is a DOM array and the other is a JS array. They don't share the same constructor or the same prototype chain.
- lhorie 5y agoconst mapNodeList = (...nodeList) => nodeList.map(myFn); mapNodeList(...$nodeList) Garbage collection really isn't the biggest of concerns here considering this code is susceptible to a stack overflow...
- kevincox 5y agoOr simply [...nodeList].map()
- moron4hire 5y agoI don't think you can use splats with a NodeList. At least, the TypeScript compiler complains about it.
- edflsafoiewq 5y agoWorks for me. Splats work for any iterable.
- webstrand 5y ago`[ ...document.body.childNodes ]` is legal. You need to have the `DOM.Iterable` enabled in your compilerOptions.libs` for TypeScript to permit it, though.