News for hackers. Stuff that matters.
Re:HTML5 Deck of Cards by pakastin on Thursday September 03, 2015 @09:39AM (#10164844)
I think "value | 0" is basically same as "Math.floor(value)"
Re:HTML5 Deck of Cards by ZitchDog on Thursday September 03, 2015 @09:53AM (#10164936)
Yes, but it is 18% faster do run the bitwise or on my machine:

https://jsperf.com/or-vs-floor/2

Re:HTML5 Deck of Cards by Stormcaller on Thursday September 03, 2015 @10:42AM (#10165189)
Its because they don't do the same thing

Math.floor() favors the number equal to/less than the parameter, Math.floor(-15.5) is -16 while (-15.5 | 0) is -15

Also because the returned value is int32[0],

Math.floor(2147483648.5) is 2147483648 while (2147483648.5 | 0) is -2147483648

You are fine if you know the input is less than (2^31) + 1 and you want to truncate, rather than floor.

[0]: http://www.ecma-international.org/ecma-262/6.0/#sec-binary-b...

Re:HTML5 Deck of Cards by nkron on Thursday September 03, 2015 @12:14PM (#10165769)
ECMAScript 2016 adds Math.trunc()[0] which should give the same value as | 0 for inputs that fit in 32 bits.

[0] http://www.javascripture.com/Math#trunc

Re:HTML5 Deck of Cards by 4Data on Thursday September 03, 2015 @02:20PM (#10166655)
Hmm, pretty sure floor rounds a number downward to its nearest integer. So Math.floor(-15.5) would be 15.

https://developer.mozilla.org/pt-PT/docs/Web/JavaScript/Refe...

Re:HTML5 Deck of Cards by Stormcaller on Thursday September 03, 2015 @03:21PM (#10166977)
Literally the first example on that page:

> Math.floor( 45.95); // 45

> Math.floor(-45.95); // -46

Its not like its hidden or anything... Its in the center of the page on my 1920x1080 screen.

Re:HTML5 Deck of Cards by catchmrbharath on Thursday September 03, 2015 @02:28PM (#10166702)
Nope. Math.floor(-15.5) would be -16. It rounds a number downwards. -16 < -15.
Re:HTML5 Deck of Cards by jpollock on Thursday September 03, 2015 @11:28AM (#10165466)
There is no substantial difference in performance on mine (within 2%), although the OR was always a little bit faster.

Chrome 45 on Linux

It's odd that a bitwise operator should have the effect of truncating the float (since X|0 == X)? I'm guessing there's an implicit type conversion to int in the middle?

Re:HTML5 Deck of Cards by TazeTSchnitzel on Thursday September 03, 2015 @11:38AM (#10165526)
The bitwise operators don't make sense for floating-point values. So they work by converting to a 32-bit integer first.
Re:HTML5 Deck of Cards by aaronm67 on Thursday September 03, 2015 @03:24PM (#10166992)
And every time a developer sees that, they're going to google "bar javascript" "single bar javascript" "bitwise or javascript" "bitwise or javascript effect" until they figure out WTF is going on.

This all to save a fraction of a microsecond on an operation that's called 60 times on the page.

Re:HTML5 Deck of Cards by pkstn on Thursday September 03, 2015 @05:40PM (#10167686)
Actually only 52 :D

But yeah, I agree about googling part: I remember having Googled "tilde javascript" and "pipe javascript".. :)

Tilde is useful with indexOf:

if (~array.indexOf(item)) {}

..equals to:

if (array.indexOf(item) > -1) {}

Re:HTML5 Deck of Cards by yellowapple on Thursday September 03, 2015 @08:47PM (#10168289)
This is the sort of thing that makes Perl readable in comparison to Javascript :)
Re:HTML5 Deck of Cards by ldesegur on Thursday September 03, 2015 @10:34AM (#10165144)
i/13|undefined works too
Re:HTML5 Deck of Cards by logicallee on Thursday September 03, 2015 @12:11PM (#10165744)
plus

  i/13|undefined  
has that "this was originally written in javascript, not ported halfheartedly from some other language" feel. (Well, maybe PHP.)
Re:HTML5 Deck of Cards by ygra on Thursday September 03, 2015 @02:14PM (#10166607)
That's just adding another conversion from undefined to Number to Int32 for the right operand. You could probably use false as well. Or the empty string.
Re:HTML5 Deck of Cards by irascible on Friday September 04, 2015 @01:39AM (#10168971)
gulp I guess I should stop using parseInt :/
Re:HTML5 Deck of Cards by robocat on Monday September 07, 2015 @03:14AM (#10180419)
value | 0 will always return a 32 bit integer.

Math.floor(value) can return some weirder values such as: NaN (for anything that isn't a number), and Infinity, -Infinity, -0. And Math.floor(-1e-300); is -1 so care needs to be taken with floating values near zero.