Skip to content

Instantly share code, notes, and snippets.

@aprilmintacpineda
Last active November 26, 2018 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprilmintacpineda/099d7becc86dfbced84893e471a42bba to your computer and use it in GitHub Desktop.
Save aprilmintacpineda/099d7becc86dfbced84893e471a42bba to your computer and use it in GitHub Desktop.
JS code speed tests

Looping through elements

http://jsben.ch/UgRvN

  • Accesses length property every loop.
  • Accesses length property only once and saves it into a constant.
  • Uses forEach method.

Removing an element of an array

http://jsben.ch/Io731

  • array.splice.
  • array.filter.
  • Manually looping through each of the element to create a new array.

Notes

  • Unlike array.filter, array.splice mutates the original array. So these two have different use cases. http://jsben.ch/DA7U2 compares array.filter and a regular for-loop.

array.map vs for-loop

http://jsben.ch/coXD5

array.forEach vs for-loop

http://jsben.ch/XuoTX

for-loop vs while-loop

http://jsben.ch/GjSyQ

Checking if object has a particular key

  • in operator
  • strict comparison operator
  • Object.hasOwnProperty
  • object.key expression

http://jsben.ch/EwSf2

Credits to who ever made it. I simply added a new code block.

Checking if an element exists in an array

http://jsben.ch/JLngr

  • array.indexOf
  • array.includes
  • for-loop
  • while-loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment