DEV Community

Discussion on: Stop abusing .map()!

Collapse
xtofl profile image
xtofl

I can imagine a Javascript optimizer looking at { xs.map(f) } and deciding that, since the mapped return value is not used, and there can be no side-effect from f, it might as well omit executing the whole block.

Or is there something in the standard that enforces map to actually run?

Collapse
totally_chase profile image
Phantz

It would be valid optimization in certain languages. But V8 won't do this unless it can prove that the mapping function is pure. That's very difficult unless certain functions are explicitly marked as "pure". As far as I'm aware of, V8 employs no such technique. So V8 would rather lean on the safe side and not erase operations.

But in general, optimizations must not modify perceivable behavior of a program. I've noticed the exact definition of "perceivable" be different in different languages and implementations though. In the context of JS and V8, I've never seen redundant mappings get optimized away.