Skip to content

Instantly share code, notes, and snippets.

@kTmnh
Last active May 13, 2016 09:26
Show Gist options
  • Save kTmnh/4597497 to your computer and use it in GitHub Desktop.
Save kTmnh/4597497 to your computer and use it in GitHub Desktop.
JavaScript techniques including bitwise operator replacement techniques, shortcuts, etc.
var PI = Math.PI;
//Faster replacement for Math object methods.
Math.round(PI) === PI + (PI < 0 ? -0.5 : +0.5) >> 0;
Math.ceil(PI) === PI + (PI < 0 ? -1 : 0) >> 0;
Math.floor(PI) === PI + (PI < 0 ? -1 : 0) >> 0;
//Conditional operator is faster than Math object methods.
Math.max(a, b) === (a > b) ? a : b;
Math.min(a, b) === (a < b) ? a : b;
Math.abs(n) === n = n > 0 ? -n : n;
var array = [1, 5, 3, 2, 4];
//Sort Array in ascending order
array.sort(function (a, b) { return a - b});
//Sort Aarray in descending order
array.sort(function (a, b) { return b - a});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment