
A shorter way to do Math.floor – A little known trick - mbrubeck
http://www.tapper-ware.net/blog/?p=112
======
bshep
Interesting, but if someone else sees your code they wont know what is going
on.

Also, it only gives the right answer for positive numbers, its off by one on
negative numbers.

~~~
BudVVeezer
When I read about these sort of hacks to get performant JS code, it reminds me
of how things used to be thirty years ago. Sacrifice code readability to get a
speed increase because of poor tools...

~~~
robin_reala
If you look at the comments you’ll see some test results. The slowest result
was 38ms / 100,000 iterations. Even if you looked at that result on a mobile
device it still wouldn’t be what you’d call unperformant.

In other words, this is micro-optimisation at a pointless level.

~~~
BudVVeezer
I don't disagree with your point. I was speaking more towards the general
trend. It seems like I see one of these articles every week or so.

------
silvestrov
Unusable micro-benchmark. The JIT can compile everything away. From the code
in <http://ernestdelgado.com/js/main.js>

    
    
      function doubletilda() {
        ~~0.12341234;
        ~~0.2;
        ~~-3.1234;
        ~~100000.23;
        ~~2.12341234123412341324;
        ~~100.0000000000000000001;    
      }
    
      function pipe() {
        0.12341234|0.12341234;
        0.2|0.2;
        -3.1234|-3.1234;
        100000.23|100000.23;
        2.12341234123412341324|2.12341234123412341324;
        100.0000000000000000001|100.0000000000000000001;    
      }
    

The JIT should be able to optimize the function bodies away.

And the loop is:

    
    
      // var fn = doubletilda;
      for (var n = 0, t = 0; t < 1000; n++) {
        fn();
        t = new Date() - start;
      }
    

So he's basically just testing creating objects and GC.

------
aidenn0
Hmm Math.floor(3e100) -> 3e100 0|3e100 -> 0 This is really just a truncate
that only works on numbers between MIN_INT and MAX_INT.

------
kwamenum86
That is really truncation and the result happens to be the same for positive
numbers

