News for hackers. Stuff that matters.
Re:HTML5 Deck of Cards by riebschlager on Thursday September 03, 2015 @09:37AM (#10164833)
Maybe it's a weird takeaway, but this line is really clever:

var suit = i / 13 | 0;

That's such a clean way to get a 0, 1, 2 or 3 from each card's `i` and I never would have thought of it.

Re:HTML5 Deck of Cards by takeda on Thursday September 03, 2015 @11:34AM (#10165497)
If you're referring to |0 then I would disagree. It shows how horrible JavaScript is. This is very confusing if you're not familiar with this trick or strange behavior of the language.

    suit = int(i / 13)
    suit = (int)(i / 13)
    suit = math.floor(i / 13)
Are much easier to understand for someone not familiar with the code.
Re:HTML5 Deck of Cards by TazeTSchnitzel on Thursday September 03, 2015 @11:37AM (#10165519)
The behaviour of JavaScript's bitwise operators isn't that unreasonable, but the excessive use of this trick for tiny performance gains at the expense of readability is a shame.
Re:HTML5 Deck of Cards by riebschlager on Thursday September 03, 2015 @11:47AM (#10165583)
I take your point, but that's some crazy non-JS typecasting in your first two examples :)
Re:HTML5 Deck of Cards by fenomas on Thursday September 03, 2015 @09:40PM (#10168425)
Languages are what they are, and if you're going to use one you might as well use it idiomatically. Maybe it's just me, but I see "|0" all the time in JS, not as some kind of crazy performance hack, but used idiomatically to mean "truncate".

And on the flip side with JS's lack of number types, any code that looks like it's doing type casts (rather than math) looks out of place. If one must mess around with JS number types then all you can really do is give hints to the optimizing compiler, in which case "|0" is the standard way to hint that you want a small int.

Obviously what looks clean is a matter of opinion, but FWIW.

Re:HTML5 Deck of Cards by adamkochanowicz on Thursday September 03, 2015 @12:53PM (#10166006)
suit = Integer(i / 13) suit = Math.floor(i / 13)
Re:HTML5 Deck of Cards by daok on Thursday September 03, 2015 @11:22AM (#10165427)
1.89% faster than Math.Floor, 100% more hard to read.
Re:HTML5 Deck of Cards by adamc on Thursday September 03, 2015 @02:28PM (#10166705)
I used http://jsperf.com/or-vs-floor/2 in Firefox 40.0.3 and it was only 0.37% faster. Was similar in Chrome.
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.

Re:HTML5 Deck of Cards by DatBear on Thursday September 03, 2015 @12:17PM (#10165783)
Why not just i % 4?
Re:HTML5 Deck of Cards by novaleaf on Thursday September 03, 2015 @12:29PM (#10165860)
that would alternate the suit for each card, and all the aces would be the same suit, etc.
Re:HTML5 Deck of Cards by epyjao on Thursday September 03, 2015 @02:17PM (#10166636)
If you also take the cards modulo 13 for their face, this approach works.

4 and 13 are relatively prime, so each number between 1 and 52 can be uniquely represented by the function (x) -> (x%13, x%4).

Re:HTML5 Deck of Cards by DatBear on Saturday September 05, 2015 @11:41AM (#10175107)
Yes, obviously it's not a drop in replacement as the behavior is different, but it does the same thing, giving you a number from 0 to 3 based on the card number. Pairing it with i%13 works just fine.
Re:HTML5 Deck of Cards by gcb0 on Thursday September 03, 2015 @06:43PM (#10167930)
on my deck hacks i use proper data structures but on the initializer for the deck i have `suit = i % 4`.

which has the advantage of working with any size of deck. important since in brazillian truco you leave most of the numbers out ;)

Re:HTML5 Deck of Cards by hacker_9 on Thursday September 03, 2015 @09:56AM (#10164957)
In asmjs you see this sort of syntax 'num | 0' a lot [0] to enforce that the number is an int, so that may be where he got the idea.

[0] http://www.sitepoint.com/understanding-asm-js/

Re:HTML5 Deck of Cards by Stormcaller on Thursday September 03, 2015 @11:04AM (#10165309)
It exists in other languages, too, like PHP.
Re:HTML5 Deck of Cards by TazeTSchnitzel on Thursday September 03, 2015 @11:29AM (#10165474)
You can do this in PHP, but you shouldn't.

Use (int), like C.

Re:HTML5 Deck of Cards by Stormcaller on Thursday September 03, 2015 @12:20PM (#10165804)
It is not much worse than using it in javascript. People do this in js because it saves a few keystrokes and its faster, not that it matters. Its a small thing. Its dirtier, for sure.

I just tested it in PHP and it seems it is 20% faster in PHP compared to casting to int. Maybe someone out there will have a weird use case for that, or someone doesn't want to put more effort into writing code into terminal and doesn't care about his code being unreadable. I think its useful to know it exists.

Btw, do you or someone else know why its faster in php too? Its a bitwise operation and I assume under the hood its casting to int, its weird to have it faster than casting it to int, why does this happen? It makes sense in javascript to that its faster than functions, but why it is faster than a cast in PHP?

Re:HTML5 Deck of Cards by TazeTSchnitzel on Thursday September 03, 2015 @01:49PM (#10166446)
> It is not much worse than using it in javascript.

It is worse because people are even less familiar with this in PHP. It's not idiomatic.

> I just tested it in PHP and it seems it is 20% faster in PHP compared to casting to int.

How rigorous was your testing? Which version of PHP were you using?

> It makes sense in javascript to that its faster than functions, but why it is faster than a cast in PHP?

It shouldn't be. It might save an allocation in older PHP versions, I'm not sure.

Re:HTML5 Deck of Cards by redler on Thursday September 03, 2015 @02:26PM (#10166689)
Putting aside whether this sort of thing is clever and clean or occult and confusing, another pattern of the same ilk is:

  ~~(i/13)
Re:HTML5 Deck of Cards by tracker1 on Thursday September 03, 2015 @05:07PM (#10167529)
for me I see `~~` and I think coerce to integer value (the result will be an integer regardless of i's value, 0 if it fails a clean coercion)

Similar to seeing `!!` and thinking true/false coercion.