r/javascript • • May 07 '16

12 extremely useful hacks for JavaScript

https://blog.jscrambler.com/12-extremely-useful-hacks-for-javascript/
0 Upvotes

3 comments sorted by

View all comments

1

u/gkx May 08 '16
  1. 5 is probably not very useful and pollutes the namespace (benchmarks? I'd be surprised if this actually sped anything up at all). In most cases, you should be using forEach anyways.

  2. 4 is a bad idea if the argument could be falsey.

  3. Don't do #6. Use hasOwnProperty. If you don't actually want to check that, just use document.querySelector (in the example) or document.querySelector != null if the property could possibly be falsey (not true in this case)

  4. Mistake in #10; it doesn't create a new array. The article shows it, but it says it wrong ("which instead creates a new array")

1

u/ascw May 08 '16

6) #12 is also pretty bad. Not only is it super inefficient, but if you run it a couple thousand times and test to see which element from your original array comes first, you'll find that there isn't an even chance of each value coming up.