Last active
May 13, 2016 09:26
-
-
Save kTmnh/4597497 to your computer and use it in GitHub Desktop.
JavaScript techniques including bitwise operator replacement techniques, shortcuts, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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