JS Swapping trick #javascript #js #devtips

preview_player
Показать описание
Background music:
Рекомендации по теме
Комментарии
Автор

You can do console.log({a}) instead of what you’re doing to log your variables with a tag

armandopenatamayo
Автор

While it’s true that this works, if you pulled this in a sufficiently large iterative loop, there’d be a significant and absolutely not inconsequential spike in Garbage Collection induced latency.

Just go with the temp variable.

For example, performing this trick in a standard sorting algorithm implementation, and then using it to sort a sizeably large Array, would absolutely incur a performance hit.

Be mindful of unnecessary Object creation and subsequent garbage collection. Prefer mutation to instantiation when no external holders of an object reference exist.

jerichaux
Автор

Why not produce new variables instead of flipping the values?, I can't think in a single use case for this.

sebastianespinosa
join shbcf.ru