toInt.js 460 B

1234567891011121314151617
  1. /**
  2. * "Convert" value into an 32-bit integer.
  3. * Works like `Math.floor` if val > 0 and `Math.ceil` if val < 0.
  4. * IMPORTANT: val will wrap at 2^31 and -2^31.
  5. * Perf tests: http://jsperf.com/vs-vs-parseint-bitwise-operators/7
  6. */
  7. function toInt(val){
  8. // we do not use lang/toNumber because of perf and also because it
  9. // doesn't break the functionality
  10. return ~~val;
  11. }
  12. module.exports = toInt;